The Local state and Global state sections show examples where state data is a primitive value. Next, we will have a look into one of the most powerful features of Hookstate - the interface to a nested state of a complex object state. The interface is the same for local and global states and works equally well for both cases. Show
Accessing and mutating nested stateLet's consider the following example where a state value is an array of objects. It demonstrates how to dive into the nested state of the array and deeply-nested state of an element of the array. The state of an element is passed to a child component as a property. The child component gets and sets the deep nested state. Loading... As you can see, a state mirrors the actual properties of the corresponding state value. State of an array is an array of states. State of an object is an object of states. We can deal with a state of an object like with any other variable, including passing it as a component property, like in the example above. We can dive to the deeply nested states of primitive values and set it, like we set the name property of a task in the 7 component: 8.We can also set a state of an object to the entire new object. In the example above, we append new element to the state of tasks, using the 9 method: 0.state.set({ a: 2, b: 3 })1 state methodYou may have noticed that a state object mixes properties from the state value object (eg. 2 property from 3 state) and state methods (eg. 9 property from interface). It is very likely that names of properties from your state objects will not collide with names of state methods. In other words, your objects are unlikely to have properties named, like 9, 6, 7, etc. However, it is more likely your objects may have properties with names like 8 or 9, which also exist in state methods.If the collision happens for whatever property name, properties from state methods take priority and "hide" access to nested states via 0 syntax. However, there is a 1 state method, which allows accessing nested state by name. For example 2 would give access to the same nested state as 3 in the example above. So, if it ever happens that your 4 object has got a property with the name, for example 6, which collides with the corresponding state method, you would be able to obtain the nested state behind the 6 property using nested method: 7.It is also necessary to use 1 method instead of property access by index syntax, when a property name is unknown at compile time and comes as a variable of type 9 or 0, for example, if you have got the state defined as the following, which allows for dynamic names of nested properties:
you are required to use the 1 method:
as the following would not be allowed by the typescript compiler:
However, if your dynamic property name is from a set of names which are known at compile time and do not collide with state methods, you would be able to use property access by index syntax. For example:
Below, you will find more about available methods for managing nested states. Advanced mutations for an object stateSetting new state valueLet's consider the following state:
One of the state methods is 6, which is used to set the new state value.
New state value can be also a function, which returns new value and accepts the previous one:
Learn more about in the API reference. Getting names of existing propertiesLet's consider the following state:
3 returns an array of names of existing properties. It is equivalent to 4 or 5.
Learn more about in the API reference. Updating existing propertyFor a given state:
The most efficient and recommended methods to update a nested property are the following: 0These set only property 6, so it will rerender every component where property 6 is used.Avoid the following:There are alternative, less efficient methods, resulting in the same mutation and data state. The following sets the entire object state to the new value (although only 6 property is changed), so it will rerender every component where any property of the state is used. 1The following sets only the property 6 but uses the current property value via the , which marks the property 6 as used by a component even if it was not used during the last rendering. In other words using nested property state in rendering or in action dispatch has the same effect: a component is rerendered on property update. 2Learn more about and in the API reference. Adding a new propertyFor a given state: 3The recommended methods to add a new nested property are the following: 4Notice the 4 object has got any property defined, although not every property might pass Typescript compiler check. We accessed non existing property 2 and set it's state. It represents the fact the state of 3 property is actually a defined state object, which can be used to set 3 property to a new value.It allows to add new properties to the state using the same method as is used for updating a property. Avoid the followingas it can be potentially less efficient than the above recommended methods: 5Learn more about and in the API reference. Deleting an existing propertyFor a given state: 6The recommended methods to delete a property are the following: 7Avoid the followingas it can be potentially less efficient than the above recommended methods: 8Learn more about and in the API reference. Swapping two propertiesFor a given state: 9The recommended method to swap properties is the following: 0Avoid the followingas it can be potentially less efficient than the above recommended method: 1Learn more about and in the API reference. Partial updates and deletionsYou may have noticed the usage of above. This does a partial update to the state and can insert, update and delete array elements all in one call: How do you update an array of objects in ReactJS?If you want to change some or all items of the array, you can use map() to create a new array. The function you will pass to map can decide what to do with each item, based on its data or its index (or both).
How do you update nested objects?How to Update Nested Properties by String In JavaScript. const setProperty = (obj, path, value) => { const [head, ... ... . const obj = { property: { updated: false } } const updatedObj = setProperty(obj, 'property.updated', true) // The above will return: { property: { updated: false } }. const [head, .... How do you update an array in React state functional component?However, with React, we need to use the method returned from useState to update the array. We simply, use the update method (In our example it's setMyArray() ) to update the state with a new array that's created by combining the old array with the new element using JavaScript' Spread operator.
How to update array of objects in JavaScript?To update an object in a JavaScript array, you can use findIndex() method for executing each array element and updating the object values accordingly, the for loop method for iterating through an array and updating the specified value, and map() method for mapping the updated value to an object.
|