Home > database >  Render Error : undefined is not an object (evaluating"itineraries[index].places)
Render Error : undefined is not an object (evaluating"itineraries[index].places)

Time:11-21

React.useEffect(() => {
    const index: any = Object.keys(itineraries)[0];
    setSelectedItinerary(itineraries[index].places as ItineraryPlace[]);
    setSelectedDay(index);
    () => setSelectedItinerary([]);
  }, [itineraries]);

Render Error Undefined line 25

It works well at first when creating the first user but after that it displays the error. If I restart the app, the problem repeats as what it occurs. I have tried looking what could go wrong in the code but I have no idea what causes the error.

CodePudding user response:

itineraries might be empty or null. use ? (optional) operator to chain the nested items

 setSelectedItinerary(itineraries?.[index]?.places as ItineraryPlace[]);
  • Related