Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

setValue does nothing for a field inside a Material UI dialog

ErrorsetValue is called but the input in the dialog stays empty

Short 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.

  • Applies to React 19, react-hook-form 7, @mui/material 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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());
  2. 2Match what you see, then jump to its fix

    1. 1setValue 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. 2register 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. 3The 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

What does the error look like?

  • setValue is called but the input in the dialog stays empty
  • getValues 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 | head

2Fix

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 open

Confirmed by mui.com

Did this fix it?

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 src

2Fix

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 change

Confirmed by raw.githubusercontent.com

Did this fix it?

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 src

2Fix

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 loaded

Confirmed by raw.githubusercontent.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.