アレイに1つの項目を追加して、["50"]をコンソールに記録できます。しかし、私が2番目の値を追加しようとすると、 "currentScores.pushは関数"ではありません。これは間違ったやり方ですか?react.jsの状態配列で値をプッシュできないのはなぜですか?
class Scores extends Component {
constructor() {
super();
this.addScore = this.addScore.bind(this);
this.handleScoreChange = this.handleScoreChange.bind(this);
this.state = {
scores: [],
scoreInput: '',
};
}
addScore() {
const currentScores = this.state.scores;
const newScores = currentScores.push(this.state.scoreInput);
this.setState({ scores: newScores });
console.log(this.state.scores);
}
handleScoreChange(e) {
this.setState({ scoreInput: e.target.value });
}
render() {
const scores = this.state.scores;
return (
<input name="score" type="text" placeholder="Score" onChange={this.handleScoreChange}/>
<button onClick={this.addScore(this.state.scoreInput)}>add</button>
);
}
}
export default Scores;
<button onClick={ function(){this.addScore(this.state.scoreInput)}}> add</button>);
を試してみてください? –