Search code examples
reactjsreact-hooksreact-usememo

Cannot read properties of undefined (reading 'forEach') inside useEffect


I try to change render when there is a change with useEffect and I have this error "Cannot read properties of undefined (reading 'forEach')". when I log the console print the array and its values.

  const listMemo = useMemo(() => ({ list }), [list]);
  useEffect(() => {
    console.log(list)   //Print the array 
    console.log(listMemo)   //Print the array
    listMemo.list.array.forEach((element, i) => {
      //********************************
      //*************************************** */
      }
    });
  }, [listMemo]);

enter image description here


Solution

  • listMemo.list.forEach(

    NOT listMemo.list.array.forEach( - remove array and one of dots.