D
D
Dolerum2019-04-27 09:59:38
React
Dolerum, 2019-04-27 09:59:38

Why am I getting a TypeError: Cannot read property of undefined error in React?

Good day.
I get the error `TypeError: Cannot read property 'friends' of undefined`
I can't figure out how to fix it or what I'm doing wrong.
Swears at this.

const Sidebars = (props) => {
    let sidebarsElements = props.state.friends.map( friend => <SideItem name={friend.name} id={friend.id} key={friend.id}/> );
}

Sidebars trying to render here
<nav>
<ul>
    <h3>Friends</h3>
    <Sidebars />
    <ul>
    </ul>
    </nav>

And this is my state, here I wrote the array manually
let state = {
 sidebarList: {
    friends: [
    {id: 11, name: 'Andrew'},
  {id: 12, name: 'Aleksei'},
  {id: 13, name: 'Mikhail'},
    ]
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
A-One, 2019-04-27
@Dolerum

To access props from a child component, you need to pass them to the child itself in the parent component:

<nav>
<ul>
    <h3>Friends</h3>
    <Sidebars state={state.sidebarList} />
    <ul>
    </ul>
    </nav>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question