CharlesStover/reactn

Ability to update state with a function with globalPropertySetter

simonv3 opened this issue · 1 comments

Just looking through the code here:

https://github.com/CharlesStover/reactn/blob/master/src/use-global.ts#L85

It doesn't look like it's possible to pass a function to the second value that useGlobal returns, eg.:

const [value, setValue] = useGlobal('value');

and then

setValue(oldValue => oldValue ++);

As React.useState does allow.

This is useful for updating state without having to watch for changes in the current state.

Just want to confirm that I'm not missing something here.

You're right. This is something I desperately want, but have not done, because it would be a breaking change for anyone currently storing functions in their global state.

This would require cutting a v3.0, and the project has been stable for a while now that I don't feel it is worth making that change alone. This item is listed in the Roadmap for 3.0 issue, but it is unlikely that 3.0 will come to fruition in favor of other projects. I would be willing to bite the bullet and accept a PR that does nothing other than implement this functionality and bump the major version to 3.0.

In the meantime, to unblock, the setGlobal function does support this:

const [, setGlobal] = useGlobal();
setGlobal(old => ({...old}));