MediumPro challengeJavaScriptTypeScript

useReducer Hook

ReactHooksState

Implement useReducer(reducer, initialState) returning [getState, dispatch].
dispatch(action) calls reducer(state, action) and updates state.

solve(initial, actions) uses a counter reducer and returns state history.
Reducer: { type: 'inc' } → +1, { type: 'dec' } → -1, { type: 'set', value } → value.

Sample tests

Test #1inc twice, dec once
Input: [0,[{"type":"inc"},{"type":"inc"},{"type":"dec"}]]
Output: [0,1,2,1]
Test #2set action
Input: [10,[{"type":"set","value":100}]]
Output: [10,100]
Test #3No actions
Input: [5,[]]
Output: [5]
Test #4Mixed actions
Input: [0,[{"type":"inc"},{"type":"set","value":50},{"type":"dec"}]]
Output: [0,1,50,49]
Test #5Unknown action returns same state
Input: [0,[{"type":"unknown"}]]
Output: [0,0]