Undefined array in React method...works in other methods. HELP

Undefined array in React method...works in other methods. HELP
0.0 0


I need some help

getting an error, Uncaught TypeError: Cannot set property ‘undefined’ of undefined

the error comes in this method:

drawItems = (arr, items) =>{
        console.log("creating item")
        let x = item[0]
        let y = item[1]
        arr[x][y] = 1

Which is identical to the method that preceeds it as far as I can tell in terms of it’s syntax, values passed in, etc. The error is in this line specifically:

 arr[x][y] = 1

But when I log arr or log arr[anyInteger][anyInteger] (eg arr[9][23]) I get a value from the array.

The method just prior to this one, drawRooms, operates the same and has no errors.

If i comment out this routine call in the constructer, it runs fine.

Any ideas?


Not sure what is going on.

In the constructor, you set rooms as an empty array, then you call generateRooms passing rooms as a parameter.

Inside generateRooms you do a forEach over rooms (which is still an empty array).

When I tried replicating your code, this is what I have found…

…After some playing about in Codepen, I made the rooms and items global vars and modified the generateRooms function. Plus I moved some calls to componentWillMount to see if that made any difference…



items is an empty array.

in generateRooms you push a bunch of empty objects (let newItem = {}) into that array, and pass it into drawItems.

then you do a forEach for items, which means each item is just an empty object, right?

then you try to assign x = item[0] and y = item[1] which will both be undefined because item is an empty object and not an array

It also looks like you’re not using itemX or itemY for anything


I looked at your changes and implemented some of them. yours seems to work well although I did not add my CSS rules to your code, but I can see the output in the console seems to be working. Thanks for takign the time to play with it. I need to look over your generateRooms more closely to see what else I can update.


The code I pushed ended up being stuck between two versions of what I was tryign and I think i let my IDE make some changes too. I cleaned up a lot of the errors you mention here. Still not working for me though.

Like I repiled to JOhnny his seems ot do what I tried to so I’ll take a better look at his code.

Thanks for the input.