53
loading...
This website collects cookies to deliver better user experience
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.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.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
}));
updatedValue
, which carries the state value to update shopCart
.updatedValue
object to the new {"item1":"juice"}
value, setShopCart
can update the value of the shopCart
state object to the value in updatedValue
.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;
handleChange
function, we can handle any changes in the input field.item1
in the shopCart
object, which allows users to see its value as they make changes to it from the input field.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.const [shopCart, setShopCart] = useState({item1:"Juice", item2: "Icrecream"});
let copyOfObject = { ...shopCart }
delete copyOfObject['propertyToRemove']
setShopCart( shopCart => ({
...copyOfObject
}));
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.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;
handleClick
function, which handles any click events from the buttons attached to it.shopCart
object and create a button for each item.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.useState
.