Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is data.map not a function?

I built the following component in React:

import React, { useState } from 'react';


export default function Exemple(){

    var friend = function (id, firstName, lastName){
        this.id = id; this.firstName = firstName; this.lastName = lastName
    }

    var [data, setData] = useState( [
        new friend(1, 'jon', 'well')
    ])

    var newFriend =  new friend();

    function addFriend(e){  
        newFriend[e.target.id] = e.target.value;
    }

    function saveFriend(){
        setData(data.push(newFriend));
    }

    return( 
    <div className="my-table-friends">
         <table className="table">
    <thead>
      <tr>
        <th className="my-table-th">id</th>
        <th className="my-table-th">first name</th>
        <th className="my-table-th">last name</th>
      
      </tr>
    </thead>
        <tbody>
          
            {data.map((d)=>{return(
                <tr>
                    <td>{d.id}</td>
                    <td>{d.firstName}</td>
                    <td>{d.lastName}</td>
                </tr>)
            })}
        </tbody> 
  </table>

  

    <form>
    <label for="fname">id</label><br />
    <input type="text" id="id" name="fname" onChange={addFriend}/><br />
    <label for="fname">first name</label><br />
    <input type="text" id="firstName" name="fname" onChange={addFriend} /><br />
    <label for="lname">last name</label><br />
    <input type="text" id="lastName" name="lname" onChange={addFriend}/><br />
    <button onClick={saveFriend}>add</button>
    </form>
 
    </div>)
}

There is a table of friends here, and a form for adding friends. The table gets its data from the 'data' array and displays it using the map function. For some reason, when I update the array the array becomes an object, and gets an error that data.map is not a function. What is the explanation for this?

like image 968
נה ו Avatar asked Jul 11 '20 23:07

נה ו


People also ask

How do I fix data map is not a function?

The "TypeError: map is not a function" occurs when we call the map() method on an object that is not an array. To solve the error, console. log the value you're calling the map() method on and make sure to only call the map method on valid arrays.

What is not a function typescript map?

The "Map.has is not a function" error occurs when we call the has() method on a value that is not a Map object. To solve the error, convert the value to a Map before calling the has method or make sure to only call the method on Map objects.

What is map function?

map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.

Is not a function TypeError?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.


1 Answers

You're mutating the state and setting it to integer. The following is wrong:

setData(data.push(newFriend));

Instead, please do this:

function saveFriend(){
  const newData = [...data, newFriend];
  setData(newData);
}

Or in simple way:

function saveFriend(){
  setData([...data, newFriend]);
}

The reason why this doesn't work is, Array.push() returns the length of the array, thereby, setting the data to a number. And an integer doesn't have the map() function so you get the error.

like image 55
Praveen Kumar Purushothaman Avatar answered Oct 20 '22 19:10

Praveen Kumar Purushothaman