Spread operator and handle object using useState hooks in react

 

In this tutorial we are going to learn about what is a spread operator and how to handle objects using useState.


const Sprade = () => {

 const [myObject, setMyObject] = useState({

   myName: "tejas talkar",

   myAge: 23,

   Degree: "Bsc"

 });

 

 const ChangeObj = () => {

   setMyObject({ ...myObject, myName: "mern developer" });

 };


Here we can initialize myName, myAge, Degree. All these values are the current value of myObject .


Here also we can create a function that is ChangeObject which generates an updated value which is setMyObject. Here we only want to change  the myName value by using “ … ” these three dots we can only change one value. Which is define inside the function 

(myName: “mern developer”).


Code Link:-

Output:-





Post a Comment

0 Comments