Irtaza Rawjani
Irtaza Rawjani

Reputation: 101

Display Multi Dimensional Arrays/Matrix using react

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

Upvotes: 2

Views: 3051

Answers (2)

icc97
icc97

Reputation: 12793

You have a couple of issues in the createArray() method, you need to build up the row and then add it to the array. Plus as @ageoff says, you need to call setState rather than use this.state directly. This is your code changed to keep it as similar to what you currently have - but it should work.

createarray1 now just returns the array and the handleSubmit function sets the state.

Edit: I've now changed createarray1 so that it's a pure function and you pass in the rowCount and colCount. Now you can check that createarray1 works independently:

  createarray1(rowCount, colCount){
    let myarr = [];
    for(let i=0; i < rowCount; i++){
      let row = []
      for(let j=0; j < colCount; j++){
        let col = "1"
        row.push(col);
      }
      myarr.push(row);
    }
    return myarr;
  }

  handleSubmit(){
    this.setState({
      array1: this.createarray1(this.state.row1, this.state.col1)
    });
  }

Here's the function on it's own to show that it's creating the correct array:

let createarray1 = function(rowCount, colCount){
  let myarr = [];
  for(let i=0; i < rowCount; i++){
    let row = []
    for(let j=0; j < colCount; j++){
      let col = "1"
      row.push(col);
    }
    myarr.push(row);
  }
  return myarr;
}
console.log(createarray1(2,3));

Upvotes: 2

ageoff
ageoff

Reputation: 2828

You are directly modifying the state when you call this.state.array1.push. You should use setState(). If you are just pushing to a state value then you can use setState({array1: [...this.state.array1, row]})

Upvotes: 1

Related Questions