dillon1000/react
Commit
Browse files [Fresh] Set up initial scaffolding (#15619)
* Add a minimal failing test for hot reload * Set up scaffolding for React Fresh * Consider type family when comparing elementType Rendering an element with stale type should not cause it to remount. We only do this for FunctionComponent tag since checking is unnecessary for classes or host components. * Add support for forwardRef() Initially I thought I would compare families of inner .render functions. However, there is a corner case where this can create false positives. Such as when you forwardRef(X) the same X twice. Those are supposed to be distinct. But if we compare .render functions, we wouldn't be able to distinguish them after first reload. It seems safer to rely on explicit registration for those. This should be easy when forwardRef() call is in the same file, and usually it would be. For cases like HOCs and style.div`...` factories that return forwardRef(), we could have the __register__ helper itself "dig deeper" and register the inner function. * Show how forwardRef inner identity can be inferred The __register__ implementation can read the inner identity itself. * Add missing __DEV__ to tests * Add support for memo() (without fixing bailouts) This adds rudimentary support for memo components. However, we don't actually skip bailouts yet, so this is not very useful by itself alone. Tests have TODOs that we need to remove after bailout skipping is done. * Refactor type comparison for clarity * Hot update shouldn't re-render ancestor components unnecessarily My code had a bug where it checked for a wrong thing in a wrong set, leading us to always re-render. This fixes the checks so that we only schedule updates for things that were actually edited. * Add test coverage for memo(fn, areEqual) * Explicitly skip bailouts for hot reloading fibers This forces even memo() with shallow comparison to re-render on hot update. * Refactor scheduling update to reduce duplication * Remove unused variable in test * Don't check presence in a set while not hot reloading * Make scheduleHotUpdate() take named arguments * Don't keep unedited component types in the type => family map It's unnecessary because if they haven't been edited, there's no special reconciliation logic. * Add signatures that force remounting Signatures let us force a remount of a type even if from React's point of view, type is the same. A type has one current signature. If that signature changes during next hot update, all Fibers with that type should be deleted and remounted. We do this by mutating elementType scheduling a parent. This will be handy to force remount of mismatching Hooks, as well as failed error boundaries. For this to fully work, we'll need to add a way to skip built-in bailouts for all Fiber types. This will be the most invasive and annoying change. I did it for HostRoot in this PR but there's more. I'll add an automated test case that catches the missing bailout bailouts. * Support forced remounting for all component types This teaches all parent component types to remount their child if necessary. It also adds tests for them. * Remount effects while preserving state for hot reloaded components This makes sure that changes to *code* always propagate. It can break components that aren't resilient to useEffect over-firing, but that seems like a good constraint since you might need to add a dependency later anyway, and this helps avoid coding yourself into the corner. * Add missing __DEV__ blocks to tests * Fix unused variables in tests * Remove outdated TODO * Expose scheduleHotUpdate directly * Inline isCompatibleType * Run one check per component for invalidating deps This also makes the bailouts more targeted--no need to remount useEffect for a parent component of remounted fiber. * Resolve .type early This moves resolving to set up the right .type early instead of doing this before render. A bit more future-proof in case we want to restructure the begin phase later. ForwardRef is special because its type is a wrapper but it can be hot reloaded by itself. So we have a special overload for it that reconstucts the wrapper type if needed. * Add a Suspense todo * Use current.type !== workInProgress.type for ignoring deps This gets rid of one of the sets. * Use workInProgress.type !== current.type check for force re-render We still use a set for forced remount though. * Use wip.type !== current.type check in more places This also disables the remounting tests. They need a separate approach. * Use a dedicated remount mechanism * Add a test for offscreen trees It has a TODO because it seems like offscreen updates are incorrectly applied too soon. * Enable offscreen test now that it is fixed * Fix corner cases in the new remounting mechanism * Remount failed error boundaries on hot reload * Fix test now that act() flushes This test is manual so I don't actually want act here. * Nits * Add comments
Changed paths9 files
First-parent comparisonpackages/react-dom/src/__tests__/ReactFresh-test.internal.js AddedM packages/react-reconciler/src/ReactChildFiber.js ModifiedM packages/react-reconciler/src/ReactFiber.js ModifiedM packages/react-reconciler/src/ReactFiberBeginWork.js ModifiedM packages/react-reconciler/src/ReactFiberHooks.js ModifiedA packages/react-reconciler/src/ReactFiberHotReloading.js AddedM packages/react-reconciler/src/ReactFiberReconciler.js ModifiedM packages/react-reconciler/src/ReactFiberUnwindWork.js ModifiedM packages/react-reconciler/src/__tests__/ReactIncrementalErrorReplay-test.internal.js ModifiedPatch
Files changed
Rendering syntax-highlighted changes…