最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reactjs Uncontrolled component error in material ui radio button group - Stack Overflow

programmeradmin1浏览0评论

I am beginner in using ReactJS. I have an error regarding my ponent radio button group from material UI. I have a radio button that has a value of boolean.

When I tried to click a value in my radio button, it doesn't select but the console log from onchange function was fired and an error will display like this : Material-UI: A ponent is changing the default value state of an uncontrolled RadioGroup after being initialized. To suppress this warning opt to use a controlled RadioGroup.

Here is my full code:

<Grid item>                        
    <RadioGroup row aria-label="privateEntity" name="privateEntity" defaultValue={state.privateEntity} onChange={changePrivateEntity}>
        <FormControlLabel value={true} disabled={requestType === 'PUT' || state.agency} control={<Radio color="primary" />} label="YES" />
        <FormControlLabel value={false} disabled={requestType === 'PUT' || state.agency} control={<Radio color="primary" />} label="NO" />
    </RadioGroup>
</Grid>

Here is my onchange function:

const changePrivateEntity = (event, value) => {
    event.persist();
    // console.log(value);
    let x = value === 'true' ? true : false;    
    setState((prev) => ({
      ...prev,
      privateEntityDefined: true,
      privateEntity: x,
      inputErrors: {
        ...prev.inputErrors,
        privateEntity: false,
      },
      inputHelpTexts: {
        ...prev.inputHelpTexts,
        privateEntity: '',
      },
    }));
  };

Here is my state initialization :

const [state, setState] = useState({
    privateEntity: selectedRecord ? selectedRecord.agency.private_entity : null,
    privateEntityDefined: !!selectedRecord,
})

I am beginner in using ReactJS. I have an error regarding my ponent radio button group from material UI. I have a radio button that has a value of boolean.

When I tried to click a value in my radio button, it doesn't select but the console log from onchange function was fired and an error will display like this : Material-UI: A ponent is changing the default value state of an uncontrolled RadioGroup after being initialized. To suppress this warning opt to use a controlled RadioGroup.

Here is my full code:

<Grid item>                        
    <RadioGroup row aria-label="privateEntity" name="privateEntity" defaultValue={state.privateEntity} onChange={changePrivateEntity}>
        <FormControlLabel value={true} disabled={requestType === 'PUT' || state.agency} control={<Radio color="primary" />} label="YES" />
        <FormControlLabel value={false} disabled={requestType === 'PUT' || state.agency} control={<Radio color="primary" />} label="NO" />
    </RadioGroup>
</Grid>

Here is my onchange function:

const changePrivateEntity = (event, value) => {
    event.persist();
    // console.log(value);
    let x = value === 'true' ? true : false;    
    setState((prev) => ({
      ...prev,
      privateEntityDefined: true,
      privateEntity: x,
      inputErrors: {
        ...prev.inputErrors,
        privateEntity: false,
      },
      inputHelpTexts: {
        ...prev.inputHelpTexts,
        privateEntity: '',
      },
    }));
  };

Here is my state initialization :

const [state, setState] = useState({
    privateEntity: selectedRecord ? selectedRecord.agency.private_entity : null,
    privateEntityDefined: !!selectedRecord,
})
Share Improve this question edited Aug 24, 2021 at 7:26 Jc John asked Aug 24, 2021 at 7:19 Jc JohnJc John 1,8592 gold badges41 silver badges78 bronze badges 3
  • Is state.privateEntity initially defined, and then you are changing it? Can you update your question to include a MCRE? – Drew Reese Commented Aug 24, 2021 at 7:23
  • @DrewReese yes... I'll show it and show my initial states. – Jc John Commented Aug 24, 2021 at 7:24
  • Looks like you really want a controlled input since you are updating the value and pass an onChange handler. Change defaultValue to value. – Drew Reese Commented Aug 24, 2021 at 7:27
Add a ment  | 

1 Answer 1

Reset to default 5

Because you don't pass value for RadioGroup ponent so it'll show this warning:

Just update defaultValue to value

value={state.privateEntity}
发布评论

评论列表(0)

  1. 暂无评论