1

I am trying to accomplish a relevant simple task in React while using MUI which is:

  1. Make a component for RadioGroup
  2. Implement the component inside other one
  3. Pass the value and the handler (onChange)

Usually everything works fine before when I made my own radio buttons but when I am using MUI radio buttons, state dose not pass as a value and the handler dose not fire. Please take a look on the code snippet. Thank you in advance

import React, { Component} from 'react';
import { FormControl, FormControlLabel, RadioGroup, Radio } from '@material-ui/core';


class example extends Component{
    render(){
        return( 

            <FormControl component="fieldset">
            <RadioGroup
              value={this.props.value}
              onChange={this.props.handler}
              row aria-label="something"
              aria-label="something"
              name="radio-something-group"
               >
              <FormControlLabel value="example1" control={<Radio />}                          
              label="Example 1" />
              <FormControlLabel value="example2" control={<Radio />}                          
              label="Example 2" />
            </RadioGroup>
          </FormControl>
        );
    }
}

export default example;


import React, { Component} from 'react';
import example;


class implment extends Component{
      state = {value = "example1"}
      
      handler(e)
      {
       this.setState({ value: e.target.value});
      }
    render(){
        return( 
            <example value={value} onChange={this.handler} />
        );
    }
}

export implment;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

1 Answers1

0

Thank you for the help. I found the problem, I made a mistake on defining the props on the Radio component, Should say onChange={this.props.onChange}. Same apply for the value (hope this will help someone)