hook state set get code example
Example 1: update state in useState hook
const [someState, setSomeState] = useState(someInitialState);
setSomeState(someOtherState);
Example 2: react usestate
function Counter({initialCount}) {
const [count, setCount] = useState(initialCount);
return (
<>
Count: {count}
<button onClick={() => setCount(initialCount)}>Reset</button>
<button onClick={() => setCount(prevCount => prevCount - 1)}>-</button>
<button onClick={() => setCount(prevCount => prevCount + 1)}>+</button>
</>
);
}