Written by Ochuko Onojakpor ✏️
React was created to help developers easily and efficiently perform Document Object Model (DOM) manipulations in their browsers than the conventional way using vanilla Javascript.
One of React’s most commonly used Hooks is useState
, which manages states in React projects as well as objects’ states. With an object, however, we can’t update it directly or the component won’t rerender.
To solve this problem, we’ll look at how to use useState
when working with objects, including the method of creating a temporary object with one property and using object destructuring to create a new object from the two existing objects.
Updating an item's state in a React object
To understand how to manage an object’s state, we must update an item’s state within the object.
In the following code sample, we’ll create a state object, shopCart,
and its setter, setShopCart
. shopCart
then carries the object’s current state while setShopCart
updates the state value of shopCart
:
const [shopCart, setShopCart] = useState({});
let updatedValue = {};
updatedValue = {"item1":"juice"};
setShopCart(shopCart => ({
...shopCart,
...updatedValue
}));
We can then create another object, updatedValue
, which carries the state value to update shopCart
.
By setting the updatedValue
object to the new {"item1":"juice"}
value, setShopCart
can update the value of the shopCart
state object to the value in updatedValue
.
To take a step forward, we can create a function to wrap the removed logic triggered by submitting a form:
import React, { useState } from 'react';
function App() {
const [shopCart, setShopCart] = useState({item1:"Juice"});
const handleChange = (e) => {
let updatedValue = {};
updatedValue = {item1:e.target.value};
setShopCart(shopCart => ({
...shopCart,
...updatedValue
}));
}
return (
<div classname="App">
<h3>useState with object in React Hooks - <a href="https://www.logrocket.com">LogRocket</a></h3>
<br/>
<label>Name:</label>
<input type="text" name="item1" defaultValue={shopCart.item1} onChange={(e) => handleChange(e)}/>
<br></br>
<label>Output:</label>
<pre>{JSON.stringify(shopCart, null, 2)}</pre>
</div>
);
}
export default App;
By wrapping the logic we covered earlier in a handleChange
function, we can handle any changes in the input field.
Within the input field, let’s set the value of the input element to the value of item1
in the shopCart
object, which allows users to see its value as they make changes to it from the input field.
Next, let’s add the onChange
event handler to each input element, ensuring the handleChange
function triggers when we make any changes in the input field. And finally, we can display the current state of the shopCart
object as we make changes to it.
Removing an item from an object in React
The same technique can be used to remove an item from an object:
const [shopCart, setShopCart] = useState({item1:"Juice", item2: "Icrecream"});
let copyOfObject = { ...shopCart }
delete copyOfObject['propertyToRemove']
setShopCart( shopCart => ({
...copyOfObject
}));
By creating a copy of the shopCart
state object, we can delete an item from its copy, copyOfObject
. We can then set the state of the original object, shopCart
, to the value of the copied object, copyOfObject
, using the setter object, setShopCart
, which we defined earlier.
To take a step further, we can create a function to wrap the logic, which then triggers by clicking a button:
import React, { useState } from 'react';
function App() {
const [shopCart, setShopCart] = useState({item1:"Juice", item2:"Icrecream"});
const handleClick = (item_id,e) => {
let copiedShopCart = {...shopCart};
delete copiedShopCart[item_id];
setShopCart( shopCart => ({
...copiedShopCart
}));
console.log(shopCart);
}
return (
<div classname="App">
<h3>useState with object in React Hooks - <a href="https://www.logrocket.com">LogRocket</a></h3>
<br/>
1.{shopCart.item1}
<button onClick={(e) => handleClick("item1",e)}>delete</button>
<br/>
<br/>
{shopCart.item2}
<button onClick={(e) => handleClick("item2",e)}>delete</button>
<pre>{JSON.stringify(shopCart, null, 2)}</pre>
</div>
);
}
export default App;
Again, we wrap the logic we covered earlier in the handleClick
function, which handles any click events from the buttons attached to it.
This allows us to list both items in the shopCart
object and create a button for each item.
By attaching the handleClick
function to the buttons using the onClick
event, we can pass each item’s ID in the shopCart
object to the handleClick
function to detect which item to delete when the function triggers.
Conclusion
This article taught you how to use useState with objects, and how to update and delete items in an object using useState
.
I recommend checking out this article to learn more about useState. If you have any questions, don’t hesitate to contact me on Twitter at @LordChuks3.
Full visibility into production React apps
Debugging React applications can be difficult, especially when users experience issues that are hard to reproduce. If you’re interested in monitoring and tracking Redux state, automatically surfacing JavaScript errors, and tracking slow network requests and component load time, try LogRocket.
LogRocket is like a DVR for web apps, recording literally everything that happens on your React app. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred. LogRocket also monitors your app's performance, reporting with metrics like client CPU load, client memory usage, and more.
The LogRocket Redux middleware package adds an extra layer of visibility into your user sessions. LogRocket logs all actions and state from your Redux stores.
Modernize how you debug your React apps — start monitoring for free
Top comments (2)
useState()
isn't meant to manage non-primitive state. It works, however it can behave weirdly with objects as they are mutable. As a matter of fact, I encountered the issue in one of my side projects which led me to React's docs where I found out the preferred way (reactjs.org/docs/hooks-reference.h...) -useReducer()
. Just recently I used it to handle state of a dynamic form list and it really is more reliable and maintainable. 😃Well, for a local state, I find reducer pattern blowing things out of reasonable proportions, add Redux... result: object with 3-4 props vault-guarded across 4 files and 40 functions to mutate it, smelling like 2000's OO paradigm. Those tools have their rightful place and dozen of use cases, but this ain't one.
It's a simple rule - create and change state object only within a single scope. For everything else consider it to be immutable. To mutate create a copy then change it - again within single scope.
Delete example in the article can actually be done with one copy in a single line by spread syntax to accomplish "all except", similar to array's filter:
setShopCart( ({ item2 , ...rest }) => rest);
I usually need object props themselves so change can be made only using setter to start with, while passing setter into the sub-component to control state. Atomic setter ensures that state mutation is constrained to single scope by updating the copy of an object or creating a new one:
const [{.mouseX, mouseY }, setMousePosition] = useState({});
Cheers! :)