Warning when using material-ui radio group component
See original GitHub issueAre you submitting a bug report or a feature request?
Not sure.
What is the current behavior?
When I use a Material UI Radio Group, and don’t pass type="radio"
as a Field property I get a warning:
index.js:2178 Warning: Failed prop type: The prop `questions[0].questionId` is marked as required in `Questionnaire`, but its value is `undefined`.
The radio group works as expected in this scenario.
When I add the type=“radio” attribute, the value is passes as undefined and the radio group stops working.
What is the expected behavior?
No warning
Sandbox Link
Data structure for this value un checked (type=“radio” not present)
answerId: null
questionId: "4ff412dd-c7af-488c-8661-098cf081910d"
data structure checked
answerId: "3f787b28-190d-4879-b99c-67f7c60bb31f"
questionId: "4ff412dd-c7af-488c-8661-098cf081910d"
Code
<Field
name={`${name}.answerId`}
label={question.question}
component={RadioGroupField}
options={mapAnswers(question.answers)}
/>
class RadioGroupField extends React.PureComponent {
render() {
const {
input,
label,
options,
required,
disabled,
helperText,
horizontal,
meta: { touched, error, submitFailed },
className,
} = this.props;
return (
<FormControl
required={required}
fullWidth
margin="normal"
error={!!((touched || submitFailed) && error)}
>
<FormLabel component="label">{label}</FormLabel>
<RadioGroup
{...input}
className={cx(className, {
[styles.horizontal]: horizontal,
})}
>
{options.map(opt => (
<FormControlLabel
key={opt.value}
value={opt.value}
disabled={disabled}
control={<Radio />}
label={opt.label}
/>
))}
</RadioGroup>
<FormHelperText>{(touched && error) || helperText}</FormHelperText>
</FormControl>
);
}
}
What’s your environment?
“react-final-form”: 3.6.6 “react-final-form-arrays”: “1.0.6”, “react”: “16.6.0”,
Other information
index.js:2178 Warning: You must pass `type="radio"` prop to your Field(userAnswers[1].answerId) component.
Without it we don't know how to unpack your `value` prop - "undefined".
Issue Analytics
- State:
- Created 5 years ago
- Reactions:9
- Comments:7 (1 by maintainers)
Top Results From Across the Web
React Radio Group component - Material UI - MUI
The Radio Group allows the user to select one option from a set. Use radio buttons when the user needs to see all...
Read more >Reactjs Uncontrolled component error in material ui radio ...
I am beginner in using ReactJS. I have an error regarding my component radio button group from material UI. I have a radio...
Read more >Material-UI: Putting a required on Radio button: A component ...
MUI: A component is changing the uncontrolled value state of RadioGroup to be controlled. How can I get rid of this warning?
Read more >MUI V5: Radio & RadioGroups (Material UI Inputs for React ...
In this video we go over Material UI V5's Radio and RadioGroup input component, and how to use them with React! Similar to...
Read more >How to use Radio Component in Material UI ? - GeeksforGeeks
Radio buttons allow the user to select one option from a set. · Creating React Application And Installing Module: · Step 1: Create...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m migrating from
redux-form
and this was the first issue I ran into when testing our existing custom radio fields. I feel like having to addtype="radio"
prop to theField
component isn’t great API design - as a developer building forms I’m already passing incomponent={CustomRadioField}
, isn’t that enough to indicate it’s a radio field. We don’t have to pass intype
for any other types and it’s easy to miss off, causing this warning and in some instances the field to stop working. After studying the source I can’t think of a nicer way to change the API right now though and I can see why it was done this way.In the meantime, I have found a workaround for other people hitting this issue. If you skip passing a
SyntheticEvent
to thefieldState.input.onChange
function and instead pass the new value directly you don’t get the warning any more.Previously your
input
element might have looked like this where you’re passinginput.onChange
directly to theonChange
prop:Now we pull out the value from the
SyntheticEvent
and pass it instead:Hey Erik! The logic you explained works fine, but that’s not what this issue is about, this is about the warning. Removing or fixing this warning would allow both behaviour to work fine I think.