0

I declared arr2d as state = { arr2d: [] }. I want to access it in my render method given below. I have defined it in componentDidMount().

my render method is as follows

render() {
    return (
        <div className="App">
            <button
                onClick={() => {
                    console.log(this.state.arr2d[0][1]);
                }}
            >
                Click Me
            </button>
            {console.log(this.state.arr2d[0][1])}
        </div>
    );
}

This is my first time asking question on stackoverflow. I would really appreciate any help.Thank you.

  • You defined a 1d array by `arr2d: []` – keikai Dec 25 '19 at 13:57
  • Does this answer your question? [Declare an empty two-dimensional array in Javascript?](https://stackoverflow.com/questions/18163234/declare-an-empty-two-dimensional-array-in-javascript) – keikai Dec 25 '19 at 13:58
  • No. But thanks for the reply. I got the answer. I just added `this.state.arr2d.length > 0 ? : console.log(this.state.arr2d[0][0]) : console.log('not yet initialized') `. [https://stackoverflow.com/questions/47799576/how-to-update-state-with-setstate-before-render] this helped. – Kunal Phaltankar Dec 26 '19 at 09:06

0 Answers0