Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Propose React.useStateValue #698

Open
davesnx opened this issue Dec 16, 2022 · 0 comments
Open

RFC: Propose React.useStateValue #698

davesnx opened this issue Dec 16, 2022 · 0 comments
Labels

Comments

@davesnx
Copy link
Member

davesnx commented Dec 16, 2022

I want to re-born this PR #426 but with another approach.

Currently React.useState is a weird API https://github.com/jchavarri/reason-react/blob/6ecf6ee6fdb4412114de9eacba1b3adc517eb387/src/React.re#L116-L121 since expects the initial value to be computed once.

Most codebases I have seen there's a binding that fixes this specific issue. Maybe in a different fashion. I think similarly as @jchavarri where a hook that only holds the value matches 1-to-1 to the JS usage.

Internally at @ahrefs we don't use a binding (and therefore it might be unsafe as @rickyvetter suggest). Instead, we implement useState with a dummy useReducer (but it's not a "zero-cost" binding and is exactly the same implementation as React.js #402)

let useStateValue = initial => {
  useReducer((_ignored, newState) => newState, initial);
};

With the interface:

let useStateValue: 'state => ('state, 'state => unit)

I'm opening this PR to gather some feedback if it make sense to add it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant