Implement usePrevious
Retain the previous committed value without triggering a render.EasyHooks
15 min79.2% acceptance
Description
Implement a usePrevious hook and use it in a comparison component. It should return undefined on the first render and the prior committed value thereafter.
Requirements
usePrevious returns undefined for the first committed render.
After rerendering, usePrevious returns the value from the preceding committed render.
Updating the stored previous value does not trigger an additional component render.
The exported hook preserves the TypeScript type of primitive, object, and undefined values.
Value update
Input
Render with A, then render with B
Expected output
The hook returns A after the second render
Constraints
Do not cause an additional renderPreserve the generic value typeReturn undefined initially
Hints
Hint 1
Choose storage whose updates persist across renders without scheduling another render.
Hint 2
Read the stored value during render, but update the storage only after the current render commits.
Hint 3
Parameterize both the hook input and its nullable initial result so TypeScript preserves the caller's value type.
useRefcustom hooksrender cycle