【发布时间】:2019-02-10 21:31:25
【问题描述】:
我最近开始使用 React,但遇到了矩阵问题。列和行作为用户的输入,应该显示一个矩阵作为输出。这是我的代码:
class App extends Component {
constructor(props){
super(props);
this.state = {
array1 : [],
array2 : [],
col1: null,
row1 : null,
}
this.handleCol1Change = this.handleCol1Change.bind(this);
this.handleRow1Change = this.handleRow1Change.bind(this);
}
handleCol1Change(e){
this.setState({
col1 : e.target.value
})
}
handleRow1Change(e){
this.setState({
row1 : e.target.value
})
}
createarray1(){
for(let i=0; i < this.state.row1; i++){
let row = []
this.state.array1.push(row);
for(let j=0; j < this.state.col1; j++){
let col = "1"
this.state.array1.push(col);
}
return this.state.array1
}
}
handleSubmit(){
this.createarray1()
}
render() {
return (
<div>
<h3>Enter Dimensions</h3>
<form>
<h1>Matrix 1</h1>
<input placeholder="Columns" onChange={this.handleCol1Change}/>
<input placeholder="Rows" onChange={this.handleRow1Change}/>
<button type="submit" onSubmit={this.handleSubmit.bind(this)}>Enter Dimensions</button>
</form>
{console.log("array",this.state.array1,"array2",this.state.array2)}
</div>
);
}
}
我相信问题出在我的创建数组逻辑上。在console.log 上,它显示我的数组没有存储任何东西。关于我做错了什么的任何想法?
TIA
【问题讨论】:
-
我给出了答案,但我对您希望如何构建数组感到有些困惑。也许你可以把你完成的数组是什么样子的示例输出?
标签: javascript reactjs matrix multidimensional-array