0
新しいアレイを作成しようとしていますが、console.logを使用してthis.state.allColorをチェックすると値が変更されていることがわかりますが、新しいアレイを作成する際にthis.state.allColorの値を使用すると、それは動作しません:私のコードでsetStateの値を使用できないのはなぜですか?
はonValueChange = (e) => {
const allColor=e.target.value;
this.setState({allColor})
const boxes = new Array(this.state.allColor).fill().map(this.randomColor,this)
this.setState({boxes})
};
これは、同様に
onValueChange = (e) => {
const allColor = e.target.value;
const boxes = new Array(this.setState({allColor})).fill().map(this.randomColor,this)
console.log(boxes)
};
これはthis.state.allColoのいずれか
onValueChange = (e) => {
const allColor = e.target.value;
const allBoxes = this.setState({allColor})
const boxes=new Array(allBoxes).fill().map(this.randomColor,this)
console.log(boxes)
};
値は動作しません動作しません。 rはconsole.logを使用すると毎回正しく変更されますが、新しいArrayでその値を使用することはできません。
が、これは
onValueChange=(e) => {
const totalNumber = 4
const boxes=new Array(totalNumber).fill().map(this.randomColor,this)
this.setState({boxes})
};
私はあなたのコードを試しましたが、それは私のボックスにエラーが定義されていません。私は私の状態で空の配列としてボックスを持っています。 – Nhat
@Nhat 'new Array(..)'の部分( 'parseInt(allColor、10)')を解析しようとしましたか?おそらくそれは 'new Array'が文字列ではなく数字を期待しているからです。 – mersocarlin
あなたは正しいです。私はtypeofでallColorをチェックしています。数字の代わりに文字列です。ありがとうございます – Nhat