mirror of
https://github.com/facebook/react.git
synced 2026-02-26 18:58:05 +00:00
When dealing with optimistic state, a common problem is not knowing the
id of the thing we're waiting on. Items in lists need keys (and single
items should often have keys too to reset their state). As a result you
have to generate fake keys. It's a pain to manage those and when the
real item comes in, you often end up rendering that with a different
`key` which resets the state of the component tree. That in turns works
against the grain of React and a lot of negatives fall out of it.
This adds a special `optimisticKey` symbol that can be used in place of
a `string` key.
```js
import {optimisticKey} from 'react';
...
const [optimisticItems, setOptimisticItems] = useOptimistic([]);
const children = savedItems.concat(
optimisticItems.map(item =>
<Item key={optimisticKey} item={item} />
)
);
return <div>{children}</div>;
```
The semantics of this `optimisticKey` is that the assumption is that the
newly saved item will be rendered in the same slot as the previous
optimistic items. State is transferred into whatever real key ends up in
the same slot.
This might lead to some incorrect transferring of state in some cases
where things don't end up lining up - but it's worth it for simplicity
in many cases since dealing with true matching of optimistic state is
often very complex for something that only lasts a blink of an eye.
If a new item matches a `key` elsewhere in the set, then that's favored
over reconciling against the old slot.
One quirk with the current algorithm is if the `savedItems` has items
removed, then the slots won't line up by index anymore and will be
skewed. We might be able to add something where the optimistic set is
always reconciled against the end. However, it's probably better to just
assume that the set will line up perfectly and otherwise it's just best
effort that can lead to weird artifacts.
An `optimisticKey` will match itself for updates to the same slot, but
it will not match any existing slot that is not an `optimisticKey`. So
it's not an `any`, which I originally called it, because it doesn't
match existing real keys against new optimistic keys. Only one
direction.
92 lines
4.3 KiB
JavaScript
92 lines
4.3 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import typeof * as FeatureFlagsType from 'shared/ReactFeatureFlags';
|
|
import typeof * as ExportsType from './ReactFeatureFlags.native-fb';
|
|
import typeof * as DynamicExportsType from './ReactFeatureFlags.native-fb-dynamic';
|
|
|
|
// Re-export dynamic flags from the internal module.
|
|
// Intentionally using * because this import is compiled to a `require` call.
|
|
import * as dynamicFlagsUntyped from 'ReactNativeInternalFeatureFlags';
|
|
const dynamicFlags: DynamicExportsType = (dynamicFlagsUntyped: any);
|
|
|
|
// We destructure each value before re-exporting to avoid a dynamic look-up on
|
|
// the exports object every time a flag is read.
|
|
export const {
|
|
alwaysThrottleRetries,
|
|
enableHiddenSubtreeInsertionEffectCleanup,
|
|
enableObjectFiber,
|
|
enableEagerAlternateStateNodeCleanup,
|
|
passChildrenWhenCloningPersistedNodes,
|
|
renameElementSymbol,
|
|
enableFragmentRefs,
|
|
enableFragmentRefsScrollIntoView,
|
|
enableFragmentRefsInstanceHandles,
|
|
} = dynamicFlags;
|
|
|
|
// The rest of the flags are static for better dead code elimination.
|
|
export const disableClientCache: boolean = true;
|
|
export const disableCommentsAsDOMContainers: boolean = true;
|
|
export const disableInputAttributeSyncing: boolean = false;
|
|
export const disableLegacyContext: boolean = false;
|
|
export const disableLegacyContextForFunctionComponents: boolean = false;
|
|
export const disableLegacyMode: boolean = false;
|
|
export const disableSchedulerTimeoutInWorkLoop: boolean = false;
|
|
export const disableTextareaChildren: boolean = false;
|
|
export const enableAsyncDebugInfo: boolean = false;
|
|
export const enableAsyncIterableChildren: boolean = false;
|
|
export const enableCPUSuspense: boolean = true;
|
|
export const enableCreateEventHandleAPI: boolean = false;
|
|
export const enableMoveBefore: boolean = true;
|
|
export const enableFizzExternalRuntime: boolean = true;
|
|
export const enableHalt: boolean = true;
|
|
export const enableInfiniteRenderLoopDetection: boolean = false;
|
|
export const enableLegacyCache: boolean = false;
|
|
export const enableLegacyFBSupport: boolean = false;
|
|
export const enableLegacyHidden: boolean = false;
|
|
export const enableNoCloningMemoCache: boolean = false;
|
|
export const enableProfilerCommitHooks: boolean = __PROFILE__;
|
|
export const enableProfilerNestedUpdatePhase: boolean = __PROFILE__;
|
|
export const enableProfilerTimer: boolean = __PROFILE__;
|
|
export const enableReactTestRendererWarning: boolean = false;
|
|
export const enableRetryLaneExpiration: boolean = false;
|
|
export const enableSchedulingProfiler: boolean = __PROFILE__;
|
|
export const enableScopeAPI: boolean = false;
|
|
export const enableSuspenseAvoidThisFallback: boolean = false;
|
|
export const enableSuspenseCallback: boolean = true;
|
|
export const enableTaint: boolean = true;
|
|
export const enableTransitionTracing: boolean = false;
|
|
export const enableTrustedTypesIntegration: boolean = false;
|
|
export const enableUpdaterTracking: boolean = __PROFILE__;
|
|
export const enableUseEffectEventHook: boolean = true;
|
|
export const retryLaneExpirationMs = 5000;
|
|
export const syncLaneExpirationMs = 250;
|
|
export const transitionLaneExpirationMs = 5000;
|
|
export const enableHydrationLaneScheduling: boolean = true;
|
|
export const enableYieldingBeforePassive: boolean = false;
|
|
export const enableThrottledScheduling: boolean = false;
|
|
export const enableViewTransition: boolean = false;
|
|
export const enableGestureTransition: boolean = false;
|
|
export const enableScrollEndPolyfill: boolean = true;
|
|
export const enableSuspenseyImages: boolean = false;
|
|
export const enableFizzBlockingRender: boolean = true;
|
|
export const enableSrcObject: boolean = false;
|
|
export const enableHydrationChangeEvent: boolean = true;
|
|
export const enableDefaultTransitionIndicator: boolean = true;
|
|
export const ownerStackLimit = 1e4;
|
|
export const enableComponentPerformanceTrack: boolean =
|
|
__PROFILE__ && dynamicFlags.enableComponentPerformanceTrack;
|
|
export const enablePerformanceIssueReporting: boolean =
|
|
enableComponentPerformanceTrack;
|
|
export const enableInternalInstanceMap: boolean = false;
|
|
export const enableOptimisticKey: boolean = false;
|
|
|
|
// Flow magic to verify the exports of this file match the original version.
|
|
((((null: any): ExportsType): FeatureFlagsType): ExportsType);
|