Skip to content

Conversation

@gaearon
Copy link
Collaborator

@gaearon gaearon commented Jun 6, 2019

We compare "signatures" of components on a hot update to decide whether to preserve or throw away the state. Signatures are created from the order of Hook calls at build time. They include references to nested Hooks themselves, since a reorder inside a custom Hook should remount components containing it. But when do we read those references?

Previously, nested Hook call signatures were computed during the hot update, just before comparison with the next version. However, it's too late on React Native because of inline requires. [useFoo] becomes [require('useFoo')] so during hot update, we compare the next version with itself.

The fix is to make the signing resilient to timing. We now capture the list of Hooks during the first render of a component of a given type, instead of during the next hot update. To do this, I'm introducing another function call layer. The wrapper function is still used for wrapping — but also repurposed for marking the first render (and forcing signature calculation at that time). On next renders it becomes a noop. You can see the new compiled output in snapshots.

I verified this solves the problem on RN. Editing Hooks in another file now correctly resets state, but only if you change the inner Hook call order.

This adds a render-time signature call by making __signature__ curried. We need both calls. The init time tells us which type has which signature. The render time call says when's a good time to capture the lazy Hooks tree. This is necessary for supporting inline requires. I will do that in next commit.
@sizebot
Copy link

sizebot commented Jun 6, 2019

Details of bundled changes.

Comparing: d0e041a...ee0704d

react-fresh

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-fresh-runtime.development.js +23.0% +18.2% 5.2 KB 6.39 KB 1.97 KB 2.33 KB NODE_DEV
react-fresh-runtime.production.min.js 🔺+15.4% 🔺+13.0% 1.55 KB 1.79 KB 852 B 963 B NODE_PROD
react-fresh-babel.development.js +5.6% +3.7% 19.97 KB 21.1 KB 4.2 KB 4.36 KB NODE_DEV
react-fresh-babel.production.min.js 🔺+5.6% 🔺+2.2% 5.83 KB 6.16 KB 2.07 KB 2.12 KB NODE_PROD

Generated by 🚫 dangerJS

@gaearon gaearon force-pushed the fresh-sig-rethink branch from 68004e7 to ac9b827 Compare June 6, 2019 08:09
This ensures inline requires don't break comparisons between Hook signatures of previous and next versions by caching Hook list at the time of first render.
@gaearon gaearon force-pushed the fresh-sig-rethink branch from cd6a8fc to c9132b4 Compare June 6, 2019 08:33
Instead of a traversal during the comparison, explicitly compute full keys. This makes it easier to debug mismatches.
@gaearon gaearon force-pushed the fresh-sig-rethink branch from c9132b4 to ee0704d Compare June 6, 2019 10:18
Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@gaearon gaearon merged commit def56c9 into facebook:master Jun 6, 2019
@gaearon gaearon deleted the fresh-sig-rethink branch June 6, 2019 17:53
gaearon added a commit to gaearon/react that referenced this pull request Jun 19, 2019
* Split the signature call into two calls

This adds a render-time signature call by making __signature__ curried. We need both calls. The init time tells us which type has which signature. The render time call says when's a good time to capture the lazy Hooks tree. This is necessary for supporting inline requires. I will do that in next commit.

* Lazily compute Hook list on first render

This ensures inline requires don't break comparisons between Hook signatures of previous and next versions by caching Hook list at the time of first render.

* Refactor computing Hook signature keys

Instead of a traversal during the comparison, explicitly compute full keys. This makes it easier to debug mismatches.
rickhanlonii pushed a commit to rickhanlonii/react that referenced this pull request Jun 25, 2019
* Split the signature call into two calls

This adds a render-time signature call by making __signature__ curried. We need both calls. The init time tells us which type has which signature. The render time call says when's a good time to capture the lazy Hooks tree. This is necessary for supporting inline requires. I will do that in next commit.

* Lazily compute Hook list on first render

This ensures inline requires don't break comparisons between Hook signatures of previous and next versions by caching Hook list at the time of first render.

* Refactor computing Hook signature keys

Instead of a traversal during the comparison, explicitly compute full keys. This makes it easier to debug mismatches.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants