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;
>Solution :
You’re setting up another array inside the user state. Change the Calculation function as follow.
function Calculation()
{
setUsers([...users,{ name, salary}]);
}