skip to Main Content

I got a problem userData.map is not a function
TypeError: userData.map is not a function error Try changing again, error still doesn’t improve I encountered a problem userData.map is not a function TypeError: userData.map is not a function I tried changing variables but still no error. In the past, I used another computer to run normally. When I moved to this machine, I encountered no error. Are you sure from which part

[enter image description here](https://phpout.com/wp-content/uploads/2023/05/QYo8h.png)

2

Answers


  1. Just move console.log(userData) from useEffect to JSX to be able to see what userData looks like in each component render.

    return (
    <React.Fragment>
      {console.log(userData)}
    </React.Fragment>
    );
    

    First time it will output [] and the error does not appear, then in the next render it will output the new value of userData which is not an array so userData.map is not a function come.
    we are sure that userData is updated because userData.map is not a function does not happen when usrData is [].

    Your endpoint is probably returning an object not an array, inside this reponse, you may find your array so then you set it to setUserData().

    This has nothing to do with your different machines.

    Login or Signup to reply.
  2. I have the same problem the fix was to put a ? before .map userData?.map

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search