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

invoke-listeners-in-render #31

Merged
merged 3 commits into from
Jul 20, 2019
Merged

invoke-listeners-in-render #31

merged 3 commits into from
Jul 20, 2019

Conversation

dai-shi
Copy link
Owner

@dai-shi dai-shi commented Jul 18, 2019

When I implemented #26, I assumed it's safer to invoke listeners in useLayoutEffect.
However, this leads to "de-optimize to sync mode" [1]. And, it's actually slow [2].

This changes to invoke listeners in render. There might be some cases that prevent from properly working. I would like to know such cases. Eventually someone would try it an file an issue. Will see.

@markerikson
Copy link

This would seem to be problematic specifically because the point of Concurrent Mode is that existing in-progress trees can be thrown away.

@dai-shi
Copy link
Owner Author

dai-shi commented Jul 18, 2019

Yeah, the tree can be thrown away, but does that mean the local state (in this case the forceUpdate counts) is thrown away, which prevents re-renders?
If so, we should delete the line ref.current.state = nextState; so that the next callback triggers re-render.
FWIW, I tried this approach in the tool, and it seems good so far. I would like to create a failing scenario, if there's such a problem.

@markerikson how can we convince this approach works/fails? any ideas?

@dai-shi
Copy link
Owner Author

dai-shi commented Jul 20, 2019

Okay, I was able to at last reproduce the thrown-away behavior.
You are absolutely right, @markerikson .
I could see inconsistency with the current modification.

And, by removing ref.current.state = nextState; it works without inconsistency after the thrown-away behavior.
It is still an experiment with only one tiny counter app, but I got a little confident with this approach now.

@dai-shi
Copy link
Owner Author

dai-shi commented Jul 20, 2019

So, here's a big guess. It's natural to assume that we don't need batchedUpdates in render.

https://github.com/facebook/react/blob/b4178af81b02dfa0e898670ce564c4649fd8947f/packages/react-reconciler/src/ReactFiberWorkLoop.js#L597
Not quite sure how BatchedContext is used, but anyway you couldn't start render if it's already in render.


Just made a small example.
https://codesandbox.io/s/old-wood-n34dw
With it, I'd assume no batchedUpdates is required.

@dai-shi
Copy link
Owner Author

dai-shi commented Jul 20, 2019

Summary:
Although invoking callbacks in render sounds generally a bad idea, but my current understanding is if we carefully implement it, it works fine with the calculateChangedBits = () => 0; technique.
Unless I receive more feedback for various cases, it can't be said to be really ok, but to get feedback I should release it.
With that in mind, I'd merge this PR and encourage people to try it.

This specific change is not tied to state usage tracking, so any react-redux hooks app can be simply tried by replacing the library.

@dai-shi dai-shi merged commit 634f656 into master Jul 20, 2019
@dai-shi dai-shi deleted the invoke-listeners-in-render branch July 20, 2019 06:23
dai-shi added a commit to dai-shi/react-tracked that referenced this pull request Jul 20, 2019
dai-shi added a commit to dai-shi/use-context-selector that referenced this pull request Jul 20, 2019
@dai-shi
Copy link
Owner Author

dai-shi commented Jul 20, 2019

https://github.com/facebook/react/blob/ce883a19d845e1faf8d4e1587e7022feda66210a/packages/use-subscription/src/useSubscription.js#L51-L55
This code is invoking setState conditionally in render. Maybe, it's not so bad.

greatuber pushed a commit to greatuber/react-tracked that referenced this pull request Apr 10, 2024
sabugaa0128 added a commit to sabugaa0128/react-tracked that referenced this pull request Apr 11, 2024
HecateAkke added a commit to HecateAkke/react-tracked that referenced this pull request Apr 11, 2024
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

Successfully merging this pull request may close these issues.

2 participants