MediumPro challengeJavaScriptTypeScript

Custom useState

ReactHooksState

Implement useState(initial) returning [get, setState] where setState
accepts either a new value OR an updater function (prev) => next.

solve(initial, ops) drives state and returns history.
Op: { type: 'set', value } or { type: 'update', fn: '<source>' } (source eval'd).

Sample tests

Test #1Direct set
Input: [0,[{"type":"set","value":5}]]
Output: [0,5]
Test #2Function updater
Input: [10,[{"fn":"p => p * 2","type":"update"}]]
Output: [10,20]
Test #3Two updaters
Input: [0,[{"fn":"p => p + 1","type":"update"},{"fn":"p => p + 1","type":"update"}]]
Output: [0,1,2]
Test #4Mixed set + updater
Input: [5,[{"type":"set","value":100},{"fn":"p => p / 2","type":"update"}]]
Output: [5,100,50]
Test #5No ops
Input: [1,[]]
Output: [1]