setValue does nothing for a field inside a Material UI dialog
setValue is called but the input in the dialog stays emptyShort answer
setValue writes to a field the form already knows about. A MUI Dialog unmounts its content when closed, so a field inside it is not registered until the dialog opens. Call setValue after the dialog opens, keep the content mounted, or feed the value through useForm's values option.
Fix it fast
1Find out which cause you have
Log whether the form has the field at the moment you call setValue. If getValues returns undefined for it right after the call, the field was not registered yet.
setValue("email", "a@example.com"); console.log(getValues("email"), getValues());2Match what you see, then jump to its fix
- 1
setValue is called in the click handler that opens the dialog, and the input renders empty.The dialog content is unmounted, so the field is not registered yetFix - 2
register works for plain inputs but not for the MUI TextField, Select or Autocomplete.A MUI input is controlled and is not wired through ControllerFix - 3
The call sits in the component body, or the value comes from data that loads later.setValue is called while rendering, or the value is only set from outside the formFix
- 1
What does the error look like?
setValue is called but the input in the dialog stays emptygetValues returns undefined for the field right after setValue
Cause 1 of 3
The dialog content is unmounted, so the field is not registered yet
1Check
MUI's modal docs say the content of a modal is unmounted when closed. The input inside it only registers once the dialog is open, so a call made before that has nothing to attach to.
grep -n "<Dialog" -r src | head2Fix
Either keep the dialog content mounted, or call setValue in an effect that runs after the dialog is open.
<Dialog open={open} keepMounted onClose={handleClose}>
{/* fields */}
</Dialog>
useEffect(() => {
if (open) setValue("email", user.email);
}, [open, user.email, setValue]);3Verify
Open the dialog twice in a row; the input shows the value both times.
# manual check in the browser; getValues('email') returns the value while the dialog is openConfirmed by mui.com
Cause 2 of 3
A MUI input is controlled and is not wired through Controller
1Check
React Hook Form prefers uncontrolled native inputs and says components from libraries such as MUI need Controller. Do not call register on a field that Controller wraps.
grep -n "register(\|<Controller" -r src2Fix
Wrap the MUI component in Controller and give it a default value, so field.value is never undefined.
<Controller
name="email"
control={control}
defaultValue=""
render={({ field }) => <TextField {...field} label="Email" />}
/>3Verify
Typing in the field updates getValues('email'), and setValue now changes what the field shows.
# log getValues('email') on each changeConfirmed by raw.githubusercontent.com
Cause 3 of 3
setValue is called while rendering, or the value is only set from outside the form
1Check
The setValue docs say not to call it during render; use an event handler or an effect. If you only need to set a field from outside, Controller's docs point to the values option of useForm.
grep -n "setValue(" -r src2Fix
Move the call into an effect or handler, or pass loaded data through values.
const { control } = useForm({ values: { email: user.email } });3Verify
The field shows the loaded value once the data arrives.
# manual check: open the dialog after the data has loadedConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Register or wrap every field before you set it, and treat setValue as a write to an existing field.
- Put dialog forms in a component that owns its own useForm, so closing the dialog drops the form cleanly.
- Give every Controller a default value.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix