const [user, setUser] = useState({firstName: "AA",lastName: "BB",address: [{name: "AA",count: 0}]});
const plus = () => {setUser([ ...user.address, user.address[0].count + 1 ]);}
return (
<h1>{user.address[0].count}
)
I tried to wrap up setUser to object
2
Answers
Spread the object and inner array separately when updating the state:
In your code, you are not properly updating the state. If spread operations are confusing for you, you can use
immer
.Install
immer
using one of the following.Then you can use
immer
as follows.