Uncaught TypeError: Cannot read property 'map' of undefined at {Component}.render in ReactJs

后端 未结 3 1590
悲哀的现实
悲哀的现实 2021-01-15 20:22

I am Passing my Array to props of sidebar Component like this.and i want to access it in my child component...

When i save the code and go to browser... i am getting

3条回答
  •  终归单人心
    2021-01-15 21:03

    The props in sidebar component, you're passing as name. So, do this:

    this.props.name && this.props.name.map(...)
    

    The map method will work only if name props is not undefined. And it is necessary to check if it has value. Because at initial render (renders even with null), you may get error using map. So, checking it is a good practice.

提交回复
热议问题