我是收音机的一个组成部分,当我制作3个收音机时,radio1值为1,radio2值为2,收音机2值为3,然后,当我点击收音机1时,console.log(this.state.value)
返回什么都没有,
当我点击2号广播时,console.log(this.state.value)
给我1,
当我点击3号电台时,import React, { Component } from 'react';
import { FormGroup } from 'react-bootstrap';
import ContentWrapper from '../../Layout/ContentWrapper';
class Single extends Component {
constructor(props) {
super(props);
this.state = {
value: props.value
}
}
valueChange = (e) => {
this.setState({value: e.target.value});
console.log(this.state.value);
}
getSingleChoice = (qItems) => {
let items = Object.keys(qItems).map((k, index) => {
let item = qItems[k];
return (
<div
className="radio c-radio"
id={this.props.id + "_" + k}
key={this.props.id + "_" + k}
onChange={e => this.valueChange(e)}>
<label>
<input
type="radio"
name={"radio" + this.props.id}
value={item.value}
defaultChecked={this.state.value == item.value}/>
<em className="fa fa-circle"></em>{item.label}</label>
</div>
);
});
return (
<FormGroup>{items}</FormGroup>
);
};
render() {
let title = this.props.id + '. ' + this.props.title ';
return (
<ContentWrapper>
<div className="panel panel-default">
<div className="panel-heading">{title}</div>
<div className="panel-body">
{this.getSingleChoice(this.props.options)}
</div>
</div>
</ContentWrapper>
);
}
}
export default Single;
给我2,
有什么不对吗?
Telephony
答案 0 :(得分:2)
setState是异步的。如果要检查其结果,请使用其第二个参数并传递回调处理程序,您可以在其中实际查看已更改的状态。 例如:
this.setState({value: e.target.value}, () => console.log(this.state.value));