I have a function
getBar()
{
foo: 'value',
array: ['a', 'b', 'c']
}
getBar()
class foo extends Component {
state = {
bar: {}
};
componentDidMount(){
this.setState({
bar : getBar()
});
}
render() {
{this.state.bar.array.map((value, i) => <div class="row" key={i}>{value}</div>)}
}
}
Uncaught TypeError: Cannot read property 'map' of undefined
const
render()
Ok, so this is actually something to do with your component's lifecycle
The problem is that your render
method runs before the componentDidMount
method. So the first time your component renders your state looks like this:
{
bar: {},
}
So no array property on bar, which means you cannot map over it (which is why you get errors