question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setting value to deeply nested property

See original GitHub issue

suppose 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:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
avkonstcommented, May 26, 2020

what you want is coming in version 2. see code samples here: https://hookstate.js.org/docs/nested-state

0reactions
avkonstcommented, May 13, 2020

set(gUser.address.city, 'san antonio'); What would be the type of gUser.address.city in this example?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamically set property of nested object - javascript
The path of the property to update (the property can be deeply nested); A function that returns the value to update (given the...
Read more >
Safely Accessing Deeply Nested Values In JavaScript - Medium
This is a short post intended to show the many different ways on how to safely access deeply nested values in JavaScript. The...
Read more >
How to safely work with nested objects in JavaScript
Safely setting values for deeply nested objects. Using Lodash. We can use Lodash's set function. Below is the syntax set(object, path, value).
Read more >
Get Deeply Nested Value in JavaScript - Bits and Pieces
The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a...
Read more >
How To Safely Work With Nested Objects in JavaScript
Safely Setting Values for Deeply Nested Objects. Using Lodash. We can use Lodash's set function. Below is the syntax: set(object, path ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found