2

I am new in React , I was following the course of React. And the code as follow appeared issue like "TypeError: Cannot read property 'map' of undefined"; Can anyone tell what's going on ? Thank you so much!

import React, { Component } from 'react'

const ListGroup = props => {
    const {items,textProperty,valueProperty} = props;
    return ( 
      <ul className="list-group">
          {items.map(item =>(
         <li key={item[valueProperty]} className="list-group-item">
             {item[textProperty]}
         </li>
          ))}
          
      </ul>
     );
}
 
export default ListGroup;
1
  • That means that the items object hasn't been set (undefined reference). Commented Mar 23, 2021 at 10:29

4 Answers 4

2

You didn't seem to pass items prop to ListGroup:

<ListGroup items=[] />

Alternatively, you can assign a default value to items:

const {items = [],textProperty,valueProperty} = props;

Or use elvis operator:

items?.map(item => {})
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Pavlo ,thank you so much for your answer ,I had passes the items in my another file as : <ListGroup items={this.setState.genres} textProperty ="name" valueProperty ="_id" onItemSelect={this.handleGenreSelect}/>; state = { movies : [], genres :[], pageSize :4, currentPage : 1 } ; but dosen't work ...
@YellowClock this looks off, you probably want: items={this.state.genres}
You got me Pavlo!! Thank you so much!!
1

Your items object is undefined at the start. Just add a null check:

const ListGroup = props => {
    const {items,textProperty,valueProperty} = props;
    return ( 
      <ul className="list-group">
          {items && items.map(item =>(
               <li key={item[valueProperty]} className="list-group-item">
                 {item[textProperty]}
               </li>
          ))}
          
      </ul>
     );
}

6 Comments

thank you so much ,I am new on posting a question on stack overflow also ..
no problem, if this helped you make sure to accept it as the answer :) happy coding
Why did you unaccept my earlier answer, which works. You can vote up for other helpful answers, but you should accept the earliest working answer. items && items.map and items?.map do the same thing, they execute the map function if items is not falsy
Hi Sinan Yaman ,I am so sorry , I didn't notice I unaccepted your answers .I like your anser ,and It works for me .Thank you so much and sorry again ..
Thank you so much for your suggestion and understanding ! Now I understand what was going on ... It won't happen more : ) Good day !
|
1

You can use operate chaining, change your code to:

const ListGroup = props => {
    const {items,textProperty,valueProperty} = props;
    return ( 
      <ul className="list-group">

          { items?.map(item =>(
               <li key={item[valueProperty]} className="list-group-item">
                 {item[textProperty]}
               </li>
          ))}
          
      </ul>
     );
}
```

3 Comments

Thank you so much for your help Crispen !
If you find it Helpful please vote for the answer
Hi Crispen ,I had voted , however I recieved "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." I am not sure it will work ..
1

I'm taking the same course. Make sure that you have passed the right props to ListGroup in Movies class just like that:

<ListGroup
                items={this.state.genres}
                textProperty="name"
                valueProperty="_id"
                onItemSelect={this.handleGenreSelect}
/> 

Regards!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.