EasyPro challengeJavaScriptTypeScript
usePrevious Hook
ReactHooksState
Implement usePrevious(initialValue) — a hook simulation that:
- Returns a
get()function to read the previous value. - Returns an
update(newValue)function that shifts current → previous.
On the first call, get() returns null.
solve(initial, updates) creates the hook, applies updates, and returns
the recorded history of [previous, current] pairs.
const [get, update] = usePrevious(0);
// get() → null
update(1); // get() → 0
update(2); // get() → 1Sample tests
Test #1Single update
Input: [10,[20]]
Output: [[null,10],[10,20]]
Test #2Three updates, tracks previous correctly
Input: [0,[1,2,3]]
Output: [[null,0],[0,1],[1,2],[2,3]]
Test #3No updates, previous is null
Input: [5,[]]
Output: [[null,5]]
Test #4Same value repeated
Input: [1,[1,1]]
Output: [[null,1],[1,1],[1,1]]
Test #5Five updates
Input: [0,[1,2,3,4,5]]
Output: [[null,0],[0,1],[1,2],[2,3],[3,4],[4,5]]