usePrevious
React state hook that returns the previous state as described in the React Docs
Usage
Live Editor
function Demo() { const [count, setCount] = useState(0); const prevCount = usePrevious(count); return ( <div> <button onClick={() => setCount(count + 1)}>+</button> <button onClick={() => setCount(count - 1)}>-</button> <p> Now: {count}, before: {prevCount} </p> </div> ); };
Result
Loading...
API
usePrevious
Returns
T | undefined
: previous value
Arguments
Argument | Description | Type | DefaultValue |
---|---|---|---|
state | state value | T (Required) | - |