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

Allow using more or less hooks between re-renders #14834

Closed
davidbonnet opened this issue Feb 12, 2019 · 1 comment
Closed

Allow using more or less hooks between re-renders #14834

davidbonnet opened this issue Feb 12, 2019 · 1 comment

Comments

@davidbonnet
Copy link

Do you want to request a feature or report a bug?
Feature, in form of a question.
Might be infeasible. Vaguely relates to #14110.

What is the current behavior?
If a function component does use more or less hooks between re-renders, an error is thrown.
This contrasts with other implementations that allow this, such as TNG-Hooks.

What is the expected behavior?
Would it be possible to return early if values returned by hooks do not require subsequent hooks and values to be processed?
The documentation states that we must "ensure that Hooks are called in the same order each time a component renders". It seems that using more or less hooks wouldn't break that rule, as long as, during a render, the sequence of used hooks either contains or is a sub-sequence of the longest hooks sequence from all previous renders.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
v16.8.1

@gaearon
Copy link
Collaborator

gaearon commented Feb 12, 2019

Using less Hooks causes confusing semantics, e.g. some people would expect previous effects to not re-run if exited early. It's confusing enough to be worth enforcing always calling Hooks. You can put your early exit after the Hooks calls anyway.

Adding more Hooks later will likely be supported later for progressive enhancement. But we'll need to spend more time to figure out the API and best practices so for now it's disallowed.

@gaearon gaearon closed this as completed Feb 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants