【发布时间】:2019-04-25 12:00:49
【问题描述】:
我有一个使用来自react-bootstrap 的 ToggleButtonGroup 的表单。我需要根据切换中选择的值更改表单中的一些文本值。问题是当在按钮上调用onChange 时 - 我得到了之前选择的切换值,而不是新的值。
constructor(props) {
super(props);
this.tradeInput = React.createRef();
}
handleChange() {
console.log(this.tradeInput.current.props.value); // old value printed here
}
<ToggleButtonGroup type="radio" name="trade" defaultValue="BUY" ref={this.tradeInput} onChange={e => this.handleChange(e)}>
<ToggleButton value="BUY">BUY</ToggleButton>
<ToggleButton value="SELL">SELL</ToggleButton>
</ToggleButtonGroup>
有人可以建议如何解决这个问题吗?
编辑:当我尝试读取参考中的值时,发生了非常奇怪的事情:
console.log(this.tradeInput.current) 在current.props.value 中显示正确的更新值,但如果我尝试使用完全相同的输入打印console.log(this.tradeInput.current.props.value),它会给我不同的结果(旧值)。
EDIT2:完整更新的代码
class ResourceTradePanel extends React.Component {
constructor(props) {
super(props);
this.state = {
validated: false,
caps: 0,
junk: 0,
food: 0,
trade: "BUY"
};
this.handleJunkChange.bind(this);
this.handleFoodChange.bind(this);
this.handleTradeChange.bind(this);
this.updateCaps.bind(this);
this.handleSubmit.bind(this);
}
handleSubmit(event) {
...
}
handleJunkChange(event) {
this.setState({
junk: event.target.value
});
this.updateCaps();
}
handleFoodChange(event) {
this.setState({
food: event.target.value
});
this.updateCaps();
}
handleTradeChange(event) {
this.setState({
trade: event.target
});
this.updateCaps();
}
updateCaps() {
var capsInfo = 0;
const junk = parseInt(this.state.junk);
const food = parseInt(this.state.food);
if (this.state.trade === "BUY") {
capsInfo = (junk + food) * 2;
} else if (this.state.trade === "SELL") {
capsInfo = junk + food;
};
this.setState({
caps: capsInfo
});
}
render() {
return <Card>
<Card.Body>
...
<Form
id="resourceForm"
noValidate
validated={this.state.validated}
onSubmit={e => this.handleSubmit(e)}
>
<InputGroup className="mb-3">
<FormControl
name="junk"
type="number"
min={0}
max={10}
value={this.state.junk}
onChange={e => this.handleJunkChange(e)}
/>
<Form.Control.Feedback type="invalid">
<Translate id="labels.resourceLimitExceeded" />
</Form.Control.Feedback>
</InputGroup>
<InputGroup className="mb-3">
<FormControl
name="food"
ref={this.foodInput}
type="number"
min={0}
max={10}
value={this.state.food}
onChange={e => this.handleFoodChange(e)}
/>
<Form.Control.Feedback type="invalid">
<Translate id="labels.resourceLimitExceeded" />
</Form.Control.Feedback>
</InputGroup>
<InputGroup>
<FormControl
type="number"
disabled
value={this.state.caps}
/>
</InputGroup>
<ToggleButtonGroup type="radio" name="trade" defaultValue={this.state.trade} onChange={e => this.handleTradeChange(e)}>
<ToggleButton variant="outline-dark" value="BUY"><Translate id="labels.buy" /></ToggleButton>
<ToggleButton variant="outline-dark" value="SELL"><Translate id="labels.sell" /></ToggleButton>
</ToggleButtonGroup>
</Form>
</Card.Body>
</Card>
}
};
【问题讨论】:
-
你能尝试从
e.target.value获得价值吗? -
理论上,我可以,但我更喜欢使用 refs,因为我会跟踪其他输入的变化,所以我需要这个函数是通用的。
-
将
this.tradeInput.current.props.value更改为this.tradeInput.current.value -
@KOTIOS 这给了我
undefined -
看来你没有正确绑定 onChange Handler 尝试像
this.handleChange.bind(this)on onChange 一样绑定它
标签: javascript reactjs forms react-bootstrap