react-hook-form: MUI RadioGroup - setValue() doesn't update the value

Hello,

I use Material UI RadioGroup component and I would like to change the value of this radio input by using setValue() when another field in my form changes, but it doesn’t seem to have any control on the value of RadioGroup.

Here is a simple example of what I’m trying to do: https://codesandbox.io/s/withered-cloud-1xeq2

When the value of the first block changes, setValue is called to update the value of the second block but nothing happens.

Am I missing something ?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

For anyone who came here wondering how to integrate MUI RadioGroup with hook-form here is an example of handling controlled MUI RadioGroup component with react-hook-form

https://codesandbox.io/s/handling-controlled-mui-component-with-react-hook-form-run4z

@karimja here is my previous example with the custom Radio component : https://codesandbox.io/s/quiet-currying-2gy6l

I pretty much copied-paste the MUI Radio component so that it looks almost exactly the same and now I can do whatever I want with it. I hope it helps.

I had problems with 2 MUI components so far:

  • Select
  • Radio

After spending a few hours trying to make them behave like I needed, creating custom ones seemed to be a good solution for me. But it depends on your use cases of course.

Hi @MinaAmrouche,

MUI works differently, the checked prop of the <input/> in the RadioGroup component can only trigger by the user interaction.

From my experience, in order to set the value of the radio group, you have to create a custom component with state to wrap around the RadioGroup component

Thanks @bluebill1049, that example is much simpler and a comprehensive Material UI example with all the controls. I wish I had found it at https://github.com/react-hook-form/react-hook-form/tree/master/examples, would’ve saved me a few hours 😃

going to close this issue @karimja feel free to continue the discussion in here tho. thanks for your help @MinaAmrouche ❤️

Thank you for your answer @chungweileong94. I’ve decided to create my own Radio component based on MUI style and it works just fine. You’re right @bluebill1049, MUI doesn’t support uncontrolled components entirely.