feat(useMediatedState): add support for initializer and updater functions
Description
The useMediatedState hook mimics the React useState hook’s return value, which means that setState expects either a value or an updater function.
This creates a problem as the mediator function receives both values and functions, which is unintuitive and inconvenient.
This PR addresses the issue by ensuring that if setState is given an updater function, the hook will first execute this function to compute the new state. It will then pass the resulting value to the mediator. Additionally, initializer functions are now supported for improved flexibility.
Type of change
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as before)
Checklist
- [x] Read the Contributing Guide
- [x] Perform a code self-review
- [x] Comment the code, particularly in hard-to-understand areas
- [x] Add documentation
- [ ] Add hook's story at Storybook
- [x] Cover changes with tests
- [x] Ensure the test suite passes (
yarn test) - [x] Provide 100% tests coverage
- [x] Make sure code lints (
yarn lint). Fix it withyarn lint:fixin case of failure. - [x] Make sure types are fine (
yarn lint:types).
Hi team, I'd like to take on this issue. Could you please assign it to me? Thanks!
Hi @streamich, could I kindly request your review?
Hey @streamich, I would really appreciate your input.