I have this functional component which is not updating when pushing data to an array:
const Experience = (props) => {
let experience = [{
from:"",
to:"",
employer_name:"",
employer_number:""
}];
function addExperience() {
experience = [...experience, {
from:"",
to:"",
employer_name:"",
employer_number:"",
}];
}
return (
<>
{experience.map((val, idx) => {
let id = `exp-id-${idx}`
return(
<div key={idx} id={id}>
...
</div>
)
})}
<button onClick={addExperience}>Add experience</button>
</>
)
}
When Add experience is clicked the mapping is not updated, any help?
experiencein state. And for that you can either use useState hook or make this a class component and use this.state = {}