尝试React收音机,但奇怪的是收音机选择不会更新其视图。任何人都可以看看下面的代码,让我知道什么是错的?
class Test extends React.Component {
constructor(props){
super(props);
this.state = {
radioVal: "first"
}
}
handleRadioSelection(e){
e.preventDefault();
this.setState({
radioVal:e.target.value
})
}
render(){
return (
<div>
<label>
<input type="radio" onChange={this.handleRadioSelection.bind(this)}
checked={ this.state.radioVal==="first"}
value="first"
name="radio1" /> 1 <br />
</label>
<label>
<input type="radio" onChange={this.handleRadioSelection.bind(this)}
checked={this.state.radioVal==="second"}
value="second"
name="radio1" /> 2 <br />
</label>
{ "Selected: " } {this.state.radioVal}
</div>
)
}
}
React.render(<Test />, document.getElementById('container'));
答案 0 :(得分:1)
从e.preventDefault();
移除handleRadioSelection
。是否有理由首先添加该行?