.
import React, { useState } from 'react';
import './App.css'
const App = () => {
const [count1, setCount1] = useState(1)
const [count2, setCount2] = useState(2)
const [{ count3, count4 }, setCount] = useState({ count3: 8, count4: 9 })
return (
<div className="App">
<button onClick={() => {
setCount1(c => c + 1)
setCount2(c => c + 1)
}}>
Add-it</button>
<div>count1={count1}</div>
<div>count2={count2}</div>
<button onClick={() => {
setCount((c) => ({ ...c, count3: count3 + 100, count4: count4 + 100 })
)
//here "c" is currentstate and the ...c spreadoperator
}}>add obj</button>
<div>count3={count3}</div><div> count4={count4}</div>
</div>
);
}
export default App;

Comments
Post a Comment