Implement a function useMediatedState(mediator, initialState) that works like useState, but applies a mediator function whenever the state is updated.
The function should:
(newState) => newState(newState, dispatch) => voidsetState function to update the state.Example
const replaceMultipleSpaces = (s) => s.replace(/\s+/g, ' '); const [state, setState] = useMediatedState(replaceMultipleSpaces, ''); setState('hello world'); console.log(state); // 'hello world'
JavaScript Function
No test results yet
Click "Run" to execute tests