useState hook in React
Returns a stateful value, and a function to update it
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>
</>
)
}
- Next: Simple Form in React
- Previous: useReducer hook in React