react-use icon indicating copy to clipboard operation
react-use copied to clipboard

feat(useMediatedState): add support for initializer and updater functions

Open denis-pakhorukov opened this issue 1 year ago • 3 comments

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 with yarn lint:fix in case of failure.
  • [x] Make sure types are fine (yarn lint:types).

denis-pakhorukov avatar Aug 16 '24 18:08 denis-pakhorukov

Hi team, I'd like to take on this issue. Could you please assign it to me? Thanks!

Kumar06Lav avatar Aug 29 '24 08:08 Kumar06Lav

Hi @streamich, could I kindly request your review?

denis-pakhorukov avatar Dec 11 '24 19:12 denis-pakhorukov

Hey @streamich, I would really appreciate your input.

denis-pakhorukov avatar Apr 24 '25 13:04 denis-pakhorukov