setting value to deeply nested property
See original GitHub issuesuppose I have an object with the following structure:
const global = createStateLink({
user: {
name: 'some user',
hobbies: ['building', 'stuff'],
address: {
city: 'san jose',
},
},
})
in order for me to push into hobbies, I have:
const gUser = useStateLink(global)
and a button with onClick of:
gUser.nested.user.nested?.hobbies.set((hobbies) => [...hobbies, 'stuff'])
which is rather hard to read, due to nested extra path
is there a better way that is still strongly typed to do something along the lines of:
gUser.user.hobbies.set(...
Checking the example of tree state in the docs, it shows how to deal with nested properties but still with one nested level at a time
Also, in the guide for .set() it mentions a plugin called Mutate, but it’s not listed in the plugin section nor in the docs
I am not sure how the reference is checked to trigger the re-render, but is it possible to just set the whole object? as in:
gUser.set((user) => {
return {
...user,
hobbies: [...user.hobbies, 'added'],
}
})
will hookstate figure out what has changed? because in this case we technically the property name as well.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (5 by maintainers)

Top Related StackOverflow Question
what you want is coming in version 2. see code samples here: https://hookstate.js.org/docs/nested-state
set(gUser.address.city, 'san antonio');What would be the type ofgUser.address.cityin this example?