Editing for clarification that this is around a hook as this was marked as a duplicate even though the other question doesn’t use hooks. Another comment had mentioned using useEffect
so I’ll look into that for now, but if someone could demonstrate with the below or prices additional guidance that would be great!
Original:
So I have a piece of state (editableSalesperson
) set to an empty object that I want to update with an existing object using setEditableSalesperson
and pass into my ModifySalespersons
component to edit it, but the update action isn't actually updating the state.
When I run editSalesPerson
I see the object getting fed in properly in the console log, but then when it hits setEditableSalesperson(salesperson);
and I check the state value in the console log in the next line, it is still empty and hasn't changed.
Here's the code. I tried to strip it down to the relevant info, but if you need more info then I can post it.
import React from 'react';
function Salespersons(props){
const [editableSalesperson, setEditableSalesperson] = React.useState({})
function editSalesPerson(salesperson){
console.log(salesperson);
setEditableSalesperson(salesperson);
console.log(editableSalesperson);
}
return (
<>
<ModifySalespersons
isOpen={isOpen}
setIsOpen={setIsOpen}
salesperson={editableSalesperson}
onSubmit={handleSubmit}
/>
{/* code here */}
<td className="px-6 py-4 whitespace-no-wrap text-right text-sm leading-5 font-medium">
<button onClick={ () => editSalesPerson(salesperson) } className="text-indigo-600 hover:text-indigo-900">Edit</button>
</td>
</>
);
}
export default Salespersons;