I have a react component where I am trying to spread objects into the state in the constructor.
constructor() {
super()
const shapesArray = [1, 2, 3]
let renderStates = shapesArray.map((el, i) => {
return {['shape'+i]: 'black'}
})
this.state = { ...renderStates }
console.log(this.state)
}
I want to access the colors by doing this.state.shape0
,
but when I console log this.state
, I get this:
instead of Object {shape0: "black", shape1: "black", shape2: "black"}
.
What am I doing wrong here?
renderStates
is an array. Shouldn't you use[...renderStates]
?...
is not an operator!