1

I'm creating a new form with React and one of the inputs is a Material UI input. The form works great, but the console shows a Missing name at warning when the form loads even though I've put a name both on the select input and the hidden input that Material UI creates.

<FormControl variant="filled" className={classes.formControl} style={{ width: '100%' }}>
  <InputLabel id="dog-breed-label">Dog Breed</InputLabel>
  <Select
    label="Dog Breed"
    labelId="dog-breed-label"
    name="dogBreed"
    value={defaultDog.dogBreed}
    onChange={event => setBankAccount({ ...dogBreed, dogBreed: event.target.value })}
    inputRef={register({ required: true })}
    variant="filled"
    style={{ width: '100%' }}
    inputProps={{ name: 'dogBreedInput' }}
    inputRef={register({ required: true })}
  >
    <MenuItem value="CHIHUAHUA">Chihuahua</MenuItem>
    <MenuItem value="LABRADOR">Labrador Retriever</MenuItem>
  </Select>
</FormControl>

Warning Message (screenshot):

Missing name at {node: input, value: undefined, focus: ƒ}focus: ƒ focus()node: inputvalue: undefined__proto__: Object 
    in SelectInput (created by InputBase)
    in InputBase (created by WithStyles(ForwardRef(InputBase)))
    in WithStyles(ForwardRef(InputBase)) (created by FilledInput)
    in FilledInput (created by WithStyles(ForwardRef(FilledInput)))
    in WithStyles(ForwardRef(FilledInput)) (created by Select)
    in Select (created by WithStyles(ForwardRef(Select)))
    in WithStyles(ForwardRef(Select)) (at modal.js:95)
    in Modal (at bankInformation/​index.js:146)
    in Anonymous (at demographics/​index.js:199)
    in Anonymous (at routes/​index.js:16)

What do I need to add to avoid seeing this warning in the console?

4
  • Please provide a CodeSandbox that reproduces this issue. I do not see the warning here: codesandbox.io/s/dog-breed-select-fibt5. Commented Nov 13, 2019 at 14:31
  • Apologies for the delayed reply. I realized it actually happens in conjunction with react-hook-form. Here's a CodeSandbox Commented Nov 27, 2019 at 18:46
  • I think you may have forgotten to save changes in your sandbox. Changes are reflected in the output for the person editing it even before you save, but if you don't save, no one else will see those changes. When I look at your sandbox, I don't see any warning and it isn't using react-hook-form. Commented Nov 27, 2019 at 18:54
  • You're right, apologies. I've saved it now. I've been looking further just now, and it seems to be a support issue. It's been genereally addressed here but I'd still like to think there's some way to register my components. Commented Nov 27, 2019 at 19:01

2 Answers 2

4

If you are using react-hook-form version 4, I would recommend using Controller:

https://react-hook-form.com/api/#Controller

and the following code demonstrate the usage:

<Controller 
  control={control}
  name="dogBreed"
  as={<Select
    label="Dog Breed"
    labelId="dog-breed-label"
    value={defaultDog.dogBreed}
    variant="filled"
    style={{ width: '100%' }}
    inputProps={{ name: 'dogBreedInput' }}
  >
    <MenuItem value="CHIHUAHUA">Chihuahua</MenuItem>
    <MenuItem value="LABRADOR">Labrador Retriever</MenuItem>
  </Select>}
/>

also, your example have two register method in the props as well.

Sign up to request clarification or add additional context in comments.

Comments

0

This doesn't work. These plugins keep getting updated and breaking it's impossible to keep up. I'm using react-hook-form and material ui. I'm seeing the warning on load and on every keystroke.

<FormControl variant="outlined" fullWidth>
        <InputLabel style={{color: !!errors[name] ? '#f44336' : ''}}>{label}</InputLabel>
        <Controller
            control={control}
            name={name}
            as={
                <Select
                    value={value}
                    name={name}
                    onChange={handleChange}
                    label={label}
                    inputProps={{
                        name: name,
                        ref: register({
                            required: 'Required field',
                        })
                    }}
                    error={!!errors[name]}
                >
                    <MenuItem value="">
                        <em>None</em>
                    </MenuItem>
                    {options.map((key, i) =>
                        <MenuItem key={i} value={key.value}>{key.label}</MenuItem>
                    )}
                </Select>
            }
            rules={{ required: required && "Required field" }}
            defaultValue=""
        />
        <FormHelperText style={{color:'#f44336'}}>
            {!!errors[name] ? <ErrorMessage errors={errors} name={name}/> : ''}
        </FormHelperText>
    </FormControl>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.