Implement a useMediatedState hook that is similar to useState, but supports a mediator function that runs on each state update. This mediator function can be used to transform or intercept state updates.
<div>You will not be able to enter more than one space</div>
<input
type="text"
min="0"
max="10"
value={state}
onChange={(e)=>setState(e.target.value)}
/>
</div>
);
}
Arguments
mediator: A function that receives the new state and returns the transformed state. This function can have two forms:
(newState: T) => T that receives one argument, the new state dispatched by setState, and returns the final state, or
(newState: T, dispatch) => void that receives two arguments, the new state dispatched by setState and a function dispatch that will actually run the state update. It returns nothing.
initialState: The initial state value
Note: mediator should stay the same, even if it is changed to a different function.
Returns
The hook returns an array with two elements:
The current state
The setState function that updates the state. It accepts the same value and updater-function forms as the second element of the array returned by useState
Essentially, the hook returns the same values as useState.
Notes
Updater functions passed to setState should be resolved against the latest previous state.
The first mediator function passed to the hook should remain active even if a later render passes a different function.
A one-argument mediator transforms the requested state by returning the final state.
A two-argument mediator controls updates by calling the supplied dispatch function.