如何在Reactjs中获取单选按钮(react单选按钮)的值?

如何在Reactjs中获取单选按钮(react单选按钮)的值?,reactjs,radio-button,Reactjs,Radio Button,如何获取单选按钮的值,如果在RadioGroup中调用updateRadioButton,则会导致错误。我需要在控制台中使用(react单选按钮)以男性或女性身份打印。单选按钮打印正确,但无法获取值。先谢谢你 class CreateUserComponent extends React.Component { constructor(props) { super(props); this.state={radio:''} }

如何获取单选按钮的值,如果在RadioGroup中调用updateRadioButton,则会导致错误。我需要在控制台中使用(react单选按钮)以男性或女性身份打印。单选按钮打印正确,但无法获取值。先谢谢你

 class CreateUserComponent extends React.Component {
        constructor(props) {
        super(props); 
        this.state={radio:''}
    }

    updateRadioButton(e) {
        this.setState({ radio: e.target.value });
    }

    <RadioGroup horizontal>
            <RadioButton value="Male">Male</RadioButton>
            <RadioButton value="Female">Female</RadioButton>
        </RadioGroup>
类CreateUserComponent扩展了React.Component{ 建造师(道具){ 超级(道具); this.state={radio:'} } updateRadioButton(e){ this.setState({radio:e.target.value}); } 男性 女性 根据这个库的功能,
RadioGroup
有一个
onChange
prop处理程序,您可以传递它,它将返回所选的值,然后您可以将它设置为状态或传递它

下面是一个运行代码的小示例:
调试器
常量放射组=ReactRadioButtonsGroup.ReactRadioButtonsGroup;
const RadioButton=ReactRadioButtonsGroup.ReactRadioButton;
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
this.state={};
}
onRadiochange=值=>{
console.log(值);
};
render(){
返回(
男性
女性
);
}
}
ReactDOM.render(,document.getElementById(“根”);

来自:

类CreateUserComponent扩展了React.Component{ ... updateRadioButton(值){ this.setState({radio:value}); } render(){ ... 返回( 男性 女性 ) ... } }
将onChange事件添加到RadioGroup

class CreateUserComponent extends React.Component {
    constructor(props) {
    super(props); 
    this.state={radio:''};
    this.updateRadioButton = this.updateRadioButton.bind(this);
}

updateRadioButton(value) {
    this.setState({ radio: value });
}

render() {
 return(
   <RadioGroup horizontal onChange={this.updateRadioButton}>
        <RadioButton value="Male">Male</RadioButton>
        <RadioButton value="Female">Female</RadioButton>
   </RadioGroup>
 );
}
类CreateUserComponent扩展了React.Component{ 建造师(道具){ 超级(道具); this.state={radio:'}; this.updateRadioButton=this.updateRadioButton.bind(this); } updateRadioButton(值){ this.setState({radio:value}); } render(){ 返回( 男性 女性 ); }
@PremKumar没问题,iv'e在你的代码中添加了一个运行片段哇,非常感谢:-)谢谢。但是,e.target.value不起作用。updateRadioButton(value){this.setState({radio:value})你是对的。我没有注意到。我只是更新了。
class CreateUserComponent extends React.Component {
    constructor(props) {
    super(props); 
    this.state={radio:''};
    this.updateRadioButton = this.updateRadioButton.bind(this);
}

updateRadioButton(value) {
    this.setState({ radio: value });
}

render() {
 return(
   <RadioGroup horizontal onChange={this.updateRadioButton}>
        <RadioButton value="Male">Male</RadioButton>
        <RadioButton value="Female">Female</RadioButton>
   </RadioGroup>
 );
}