I started React recently and I am stuck on a matrix issue. Columns and Rows are taken as input from the user and should display a matrix as output. Here is my code:
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>
);
}
}
I believe the fault is in my create array logic. On console.log it shows that my array is not storing anything. Any ideas on what I'm doing wrong?
TIA