streamich / react-use

React Hooks — 👍
http://streamich.github.io/react-use
The Unlicense
42k stars 3.16k forks source link

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

Open denis-pakhorukov opened 3 months ago

denis-pakhorukov commented 3 months ago

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

Checklist

Kumar06Lav commented 3 months ago

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