How to use grid area to create grid layout
grid area is an important part of the grid. It helps us to create layouts easily while writing less code. Grid is used to create a 2d layout with the help of…
To update an object key in the state select the key and assign its value to that state
for example, if we have an object of books and we want to update the physics book name then we can do it in the following way
use the spread operator(…) to preserve the previous value and only update the particular key so that no other keys override
{…prev, key: value} in object literal preserve the object’s previous value and update the desired key
import { useState } from "react";
const demo = () => {
const [books, setBooks] = useState({
physics: "",
chemistry: "",
math: "",
});
return (
<div>
<p>List of books</p>
<form>
<input
type="subject"
name="book"
onClick={(e) =>
setBooks((prev) => ({
...prev,
physics: e.target.value,
}))
}
/>
<button>Update book</button>
</form>
</div>
);
};
export default demo;
In the same, we can also update the array but in the array, we will have to map to preserve other items of the array and use a filter to update that particular item of the array.
import { useState } from "react";
const demo = () => {
const [faq, setFaq] = useState([
{
que: "",
ans: "",
},
]);
return (
<div>
<p>List of books</p>
<form>
<input
type="subject"
name="que"
onClick={(e) =>
setFaq((prev) =>
prev.map((el, index) => {
if (index == i) {
return {
...el,
que: e.target.value,
};
}
})
)
}
/>
<textarea
name="ans"
onClick={(e) =>
setFaq((prev) =>
prev.map((el, index) => {
if (index == i) {
return {
...el,
ans: e.target.value,
};
}
})
)
}
/>
<button>Update book</button>
</form>
</div>
);
};
export default demo;
sometimes we are required to use nested objects in useState, in that case, it becomes difficult to update a key of an object in the useState set method. So let’s see how to update the nested object in usetstate.
const [data, setdata] = useState({
width: "160",
text: "button",
padding: "17",
border: {
width: "1",
type: "solid",
color: "red",
radius: "20",
},
background: {
isSolid: true,
color: "red",
gradient: { bg1: "red", bg2: "blue", angle: "120" },
},
textShadow: {
x: "1",
y: "-1",
blur: "1",
color: "",
},
});
above useState object is nested in 3 levels.
here I am updating the blur key of textshadow object
<input type="range"
onChange={(e) =>setdata((prev) => ({
...prev,
textShadow: {
...prev.textShadow,
blur: e.target.value,
},
}))
}
/>
Ashish Yadav is a passionate Software Engineer and technology enthusiast having 3 years of experience in web development with a deep interest in the ever-evolving world of technology.
With a background in computer science and extensive experience in software development, Ashish brings a unique perspective to his writing in the tech blogosphere & love to learn and work on technologies
grid area is an important part of the grid. It helps us to create layouts easily while writing less code. Grid is used to create a 2d layout with the help of…
Hello guys in this article we are going to see how to create moving background animation in CSS for the website. So here we are going to use @keyframe animation and translate…
System design is the process of defining systems and architectures to satisfy specific requirements. It includes the process of building architecture, components, or interfaces to satisfy the specific requirement which translates requirements…
Hello coders, in the blog we will learn how to reload a page in the React JS application Unlike the traditional web development practices where a simple page refresh could suffice, ReactJS,…
in this article, we will learn how to add an image in HTML. the syntax of adding an image in HTML is the same whether it reacts or another technology. <img/> is…