我正在尝试根据输入字段值(react js中的受控组件)设置状态,但是一旦我尝试更改输入字段的值,事件总是会导致未定义。
App.js
initialState = {
set: {
team1Score: 5,
team2Score: 6,
},
};
handleChange(e) {
this.setState({
set: {
team1Score: e.target.value,
team2Score: e.target.value,
}
});
}
render() {
return (
<Match
set = {this.state.set}
handleChange={(e) => {this.handleChange()}}>
)}
Match.js
<ScoreInput
handleChange={props.handleChange}
set={props.set}
/>
ScoreInput.js
const ScoreInput = (props) =>
<div className="c-set-input">
<input
className="c-set-input__field"
placeholder="0"
value={props.set.team1Score}
onChange={props.handleChange}
></input>
<span>:</span>
<input
className="c-set-input__field"
placeholder="0"
value={props.set.team2Score}
onChange={props.handleChange}
></input>
</div>
答案 0 :(得分:3)
您似乎忘记将参数传递给handleChange函数,请尝试使用
handleChange={(e) => this.handleChange(e)}