I am making a dynamic form component which takes input from the user and stores it in JSON format and then creates a form for the end-user. I have to dynamically add values to select tag options but one error is coming TypeError: data.emplist is not iterable
const addNewEmp=()=>{
61 | setEmpList((data)=>({
62 | inputValue: '',
> 63 | emplist: [
| ^ 64 | ...data.emplist,
65 | {
66 | empName: data.inputValue
I have done several changes but cant figure out whats wrong. My code Below
import React, { useState } from 'react'
const Select = () => {
const [inputValue,setInputValue] = useState('')
const [emplist, setEmpList] = useState([
{
empName: '---Select---'
}
]);
const addNewEmp=()=>{
setEmpList((data)=>({
inputValue: '',
emplist: [
...data.emplist,
{
empName: data.inputValue
}
]
}))
}
let empRecords = emplist.map((data) => {
return <option>{data.empName}</option>;
});
return (
<>
<input
type="text"
placeholder="add options"
onChange={(e)=> setInputValue(e.target.value)}
/>
<button onClick={addNewEmp}>Add +</button>
<br />
<select>{empRecords}</select>
{inputValue}
</>
);
}
export default Select