mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
914b57be27
* Unhide Suspense trees without entanglement When a Suspense boundary is in its fallback state, you cannot switch back to the main content without also finishing any updates inside the tree that might have been skipped. That would be a form of tearing. Before we fixed this in #18411, the way this bug manifested was that a boundary was suspended by an update that originated from a child component (as opposed to props from a parent). While the fallback was showing, it received another update, this time at high priority. React would render the high priority update without also including the original update. That would cause the fallback to switch back to the main content, since the update that caused the tree to suspend was no longer part of the render. But then, React would immediately try to render the original update, which would again suspend and show the fallback, leading to a momentary flicker in the UI. The approach added in #18411 is, when receiving a high priority update to a Suspense tree that's in its fallback state is to bail out, keep showing the fallback and finish the update in the rest of the tree. After that commits, render again at the original priority. Because low priority expiration times are inclusive of higher priority expiration times, this ensures that all the updates are committed together. The new approach in this commit is to turn `renderExpirationTime` into a context-like value that lives on the stack. Then, when unhiding the Suspense boundary, we can push a new `renderExpirationTime` that is inclusive of both the high pri update and the original update that suspended. Then the boundary can be unblocked in a single render pass. An advantage of the old approach is that by deferring the work of unhiding, there's less work to do in the high priority update. The key advantage of the new approach is that it solves the consistency problem without having to entangle the entire root. * Create internal LegacyHidden type This only exists so we can clean up the internal implementation of `<div hidden={isHidden} />`, which is not a stable feature. The goal is to move everything to the new Offscreen type instead. However, Offscreen has different semantics, so before we can remove the legacy API, we have to migrate our internal usage at Facebook. So we'll need to maintain both temporarily. In this initial commit, I've only added the type. It's not used anywhere. The next step is to use it to implement `hidden`. * Use LegacyHidden to implement old hidden API If a host component receives a `hidden` prop, we wrap its children in an Offscreen fiber. This is similar to what we do for Suspense children. The LegacyHidden type happens to share the same implementation as the new Offscreen type, for now, but using separate types allows us to fork the behavior later when we implement our planned changes to the Offscreen API. There are two subtle semantic changes here. One is that the children of the host component will have their visibility toggled using the same mechanism we use for Offscreen and Suspense: find the nearest host node children and give them a style of `display: none`. We didn't used to do this in the old API, because the `hidden` DOM attribute on the parent already hides them. So with this change, we're actually "overhiding" the children. I considered addressing this, but I figure I'll leave it as-is in case we want to expose the LegacyHidden component type temporarily to ease migration of Facebook's internal callers to the Offscreen type. The other subtle semantic change is that, because of the extra fiber that wraps around the children, this pattern will cause the children to lose state: ```js return isHidden ? <div hidden={true} /> : <div />; ``` The reason is that I didn't want to wrap every single host component in an extra fiber. So I only wrap them if a `hidden` prop exists. In the above example, that means the children are conditionally wrapped in an extra fiber, so they don't line up during reconciliation, so they get remounted every time `isHidden` changes. The fix is to rewrite to: ```js return <div hidden={isHidden} />; ``` I don't anticipate this will be a problem at Facebook, especially since we're only supposed to use `hidden` via a userspace wrapper component. (And since the bad pattern isn't very React-y, anyway.) Again, the eventual goal is to delete this completely and replace it with Offscreen.
78 lines
3.0 KiB
JavaScript
78 lines
3.0 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
// ATTENTION
|
|
// When adding new symbols to this file,
|
|
// Please consider also adding to 'react-devtools-shared/src/backend/ReactSymbols'
|
|
|
|
// The Symbol used to tag the ReactElement-like types. If there is no native Symbol
|
|
// nor polyfill, then a plain number is used for performance.
|
|
export let REACT_ELEMENT_TYPE = 0xeac7;
|
|
export let REACT_PORTAL_TYPE = 0xeaca;
|
|
export let REACT_FRAGMENT_TYPE = 0xeacb;
|
|
export let REACT_STRICT_MODE_TYPE = 0xeacc;
|
|
export let REACT_PROFILER_TYPE = 0xead2;
|
|
export let REACT_PROVIDER_TYPE = 0xeacd;
|
|
export let REACT_CONTEXT_TYPE = 0xeace;
|
|
export let REACT_FORWARD_REF_TYPE = 0xead0;
|
|
export let REACT_SUSPENSE_TYPE = 0xead1;
|
|
export let REACT_SUSPENSE_LIST_TYPE = 0xead8;
|
|
export let REACT_MEMO_TYPE = 0xead3;
|
|
export let REACT_LAZY_TYPE = 0xead4;
|
|
export let REACT_BLOCK_TYPE = 0xead9;
|
|
export let REACT_SERVER_BLOCK_TYPE = 0xeada;
|
|
export let REACT_FUNDAMENTAL_TYPE = 0xead5;
|
|
export let REACT_RESPONDER_TYPE = 0xead6;
|
|
export let REACT_SCOPE_TYPE = 0xead7;
|
|
export let REACT_OPAQUE_ID_TYPE = 0xeae0;
|
|
export let REACT_DEBUG_TRACING_MODE_TYPE = 0xeae1;
|
|
export let REACT_OFFSCREEN_TYPE = 0xeae2;
|
|
export let REACT_LEGACY_HIDDEN_TYPE = 0xeae3;
|
|
|
|
if (typeof Symbol === 'function' && Symbol.for) {
|
|
const symbolFor = Symbol.for;
|
|
REACT_ELEMENT_TYPE = symbolFor('react.element');
|
|
REACT_PORTAL_TYPE = symbolFor('react.portal');
|
|
REACT_FRAGMENT_TYPE = symbolFor('react.fragment');
|
|
REACT_STRICT_MODE_TYPE = symbolFor('react.strict_mode');
|
|
REACT_PROFILER_TYPE = symbolFor('react.profiler');
|
|
REACT_PROVIDER_TYPE = symbolFor('react.provider');
|
|
REACT_CONTEXT_TYPE = symbolFor('react.context');
|
|
REACT_FORWARD_REF_TYPE = symbolFor('react.forward_ref');
|
|
REACT_SUSPENSE_TYPE = symbolFor('react.suspense');
|
|
REACT_SUSPENSE_LIST_TYPE = symbolFor('react.suspense_list');
|
|
REACT_MEMO_TYPE = symbolFor('react.memo');
|
|
REACT_LAZY_TYPE = symbolFor('react.lazy');
|
|
REACT_BLOCK_TYPE = symbolFor('react.block');
|
|
REACT_SERVER_BLOCK_TYPE = symbolFor('react.server.block');
|
|
REACT_FUNDAMENTAL_TYPE = symbolFor('react.fundamental');
|
|
REACT_RESPONDER_TYPE = symbolFor('react.responder');
|
|
REACT_SCOPE_TYPE = symbolFor('react.scope');
|
|
REACT_OPAQUE_ID_TYPE = symbolFor('react.opaque.id');
|
|
REACT_DEBUG_TRACING_MODE_TYPE = symbolFor('react.debug_trace_mode');
|
|
REACT_OFFSCREEN_TYPE = symbolFor('react.offscreen');
|
|
REACT_LEGACY_HIDDEN_TYPE = symbolFor('react.legacy_hidden');
|
|
}
|
|
|
|
const MAYBE_ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
|
|
const FAUX_ITERATOR_SYMBOL = '@@iterator';
|
|
|
|
export function getIteratorFn(maybeIterable: ?any): ?() => ?Iterator<*> {
|
|
if (maybeIterable === null || typeof maybeIterable !== 'object') {
|
|
return null;
|
|
}
|
|
const maybeIterator =
|
|
(MAYBE_ITERATOR_SYMBOL && maybeIterable[MAYBE_ITERATOR_SYMBOL]) ||
|
|
maybeIterable[FAUX_ITERATOR_SYMBOL];
|
|
if (typeof maybeIterator === 'function') {
|
|
return maybeIterator;
|
|
}
|
|
return null;
|
|
}
|