Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

map is not a function React

i am creating the simple system in Reactjs.when i enter the data in to the form and click the button data need to be append in to the table.but when i click the button i got the problem on

users.map is not a function
TypeError: users.map is not a function

some one solve my problems i am new comer of react.what i tried so far i attached below.please rectify the error

import { useState } from "react";

function Test() {
  const [name, setName] = useState();
  const [salary, setSalary] = useState();
  const [users, setUsers] = useState([]);

  function Calculation() {
    setUsers([users, { name, salary }]);
  }

  return (
    <div class="container">
      <h3>Employee Salary Calculation</h3>

      <div class="form-group">
        <label>Employee Name</label>
        <input
          type="text"
          class="form-control"
          placeholder="Employee Name"
          onChange={(event) => {
            setName(event.target.value);
          }}
        />
      </div>

      <div class="form-group">
        <label>Salary</label>
        <input
          type="text"
          class="form-control"
          placeholder="Enter Salary"
          onChange={(event) => {
            setSalary(event.target.value);
          }}
        />
      </div>

      <button type="submit" onClick={Calculation} class="btn btn-primary mt-4">
        Submit
      </button>

      <table>
        <thead>
          <tr>
            <th>Name</th>
            <th>Email</th>
          </tr>
        </thead>
        <tbody>
          {users.map((user) => (
            <tr>
              <td>{user.name}</td>
              <td>{user.salary}</td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
}

export default Test;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re setting up another array inside the user state. Change the Calculation function as follow.

function Calculation()
{
  
  setUsers([...users,{ name, salary}]);
     

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading