Use a shared noop function from shared/noop (#33154)

Stacked on #33150.

We use `noop` functions in a lot of places as place holders. I don't
think there's any real optimizations we get from having separate
instances. This moves them to use a common instance in `shared/noop`.
This commit is contained in:
Sebastian Markbåge
2025-05-08 21:33:18 -04:00
committed by GitHub
parent 4ca97e4891
commit 21fdf308a1
15 changed files with 48 additions and 45 deletions

View File

@@ -63,6 +63,8 @@ import {validateProperties as validateInputProperties} from '../shared/ReactDOMN
import {validateProperties as validateUnknownProperties} from '../shared/ReactDOMUnknownPropertyHook';
import sanitizeURL from '../shared/sanitizeURL';
import noop from 'shared/noop';
import {trackHostMutation} from 'react-reconciler/src/ReactFiberMutationTracking';
import {
@@ -319,8 +321,6 @@ function checkForUnmatchedText(
return false;
}
function noop() {}
export function trapClickOnNonInteractiveElement(node: HTMLElement) {
// Mobile Safari does not fire properly bubble click events on
// non-interactive elements, which means delegated click listeners do not

View File

@@ -139,6 +139,9 @@ import {requestFormReset as requestFormResetOnFiber} from 'react-reconciler/src/
import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';
export {default as rendererVersion} from 'shared/ReactVersion';
import noop from 'shared/noop';
export const rendererPackageName = 'react-dom';
export const extraDevToolsConfig = null;
@@ -5628,16 +5631,14 @@ type SuspendedState = {
};
let suspendedState: null | SuspendedState = null;
// We use a noop function when we begin suspending because if possible we want the
// waitfor step to finish synchronously. If it doesn't we'll return a function to
// provide the actual unsuspend function and that will get completed when the count
// hits zero or it will get cancelled if the root starts new work.
function noop() {}
export function startSuspendingCommit(): void {
suspendedState = {
stylesheets: null,
count: 0,
// We use a noop function when we begin suspending because if possible we want the
// waitfor step to finish synchronously. If it doesn't we'll return a function to
// provide the actual unsuspend function and that will get completed when the count
// hits zero or it will get cancelled if the root starts new work.
unsuspend: noop,
};
}

View File

@@ -10,6 +10,8 @@
import type {EventPriority} from 'react-reconciler/src/ReactEventPriorities';
import type {HostDispatcher} from './shared/ReactDOMTypes';
import noop from 'shared/noop';
// This should line up with NoEventPriority from react-reconciler/src/ReactEventPriorities
// but we can't depend on the react-reconciler from this isomorphic code.
export const NoEventPriority: EventPriority = (0: any);
@@ -24,8 +26,6 @@ type ReactDOMInternals = {
) => null | Element | Text),
};
function noop() {}
function requestFormReset(element: HTMLFormElement) {
throw new Error(
'Invalid form element. requestFormReset must be passed a form that was ' +

View File

@@ -12,6 +12,8 @@ import type {HostDispatcher} from './shared/ReactDOMTypes';
import {NoEventPriority} from 'react-reconciler/src/ReactEventPriorities';
import noop from 'shared/noop';
type ReactDOMInternals = {
Events: [any, any, any, any, any, any],
d /* ReactDOMCurrentDispatcher */: HostDispatcher,
@@ -23,8 +25,6 @@ type ReactDOMInternals = {
) => null | Element | Text),
};
function noop() {}
const DefaultDispatcher: HostDispatcher = {
f /* flushSyncWork */: noop,
r /* requestFormReset */: noop,

View File

@@ -70,6 +70,8 @@ import {
import assign from 'shared/assign';
import noop from 'shared/noop';
// Provided by www
const ReactFiberErrorDialogWWW = require('ReactFiberErrorDialog');
@@ -206,14 +208,10 @@ function getReactRootElementInContainer(container: any) {
}
}
function noopOnRecoverableError() {
// This isn't reachable because onRecoverableError isn't called in the
// legacy API.
}
function noopOnDefaultTransitionIndicator() {
// Noop
}
// This isn't reachable because onRecoverableError isn't called in the
// legacy API.
const noopOnRecoverableError = noop;
const noopOnDefaultTransitionIndicator = noop;
function legacyCreateRootFromDOMContainer(
container: Container,

View File

@@ -18,6 +18,8 @@ import {getWorkInProgressRoot} from './ReactFiberWorkLoop';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import noop from 'shared/noop';
opaque type ThenableStateDev = {
didWarnAboutUncachedPromise: boolean,
thenables: Array<Thenable<any>>,
@@ -95,8 +97,6 @@ export function isThenableResolved(thenable: Thenable<mixed>): boolean {
return status === 'fulfilled' || status === 'rejected';
}
function noop(): void {}
export function trackUsedThenable<T>(
thenableState: ThenableState,
thenable: Thenable<T>,

View File

@@ -47,6 +47,8 @@ import {
import {checkAttributeStringCoercion} from 'shared/CheckStringCoercion';
import {getFormState} from './ReactFizzServer';
import noop from 'shared/noop';
type BasicStateAction<S> = (S => S) | S;
type Dispatch<A> = A => void;
@@ -795,8 +797,6 @@ function useMemoCache(size: number): Array<mixed> {
return data;
}
function noop(): void {}
function clientHookNotSupported() {
throw new Error(
'Cannot use state or effect Hooks in renderToHTML because ' +

View File

@@ -176,6 +176,7 @@ import {
} from 'shared/ReactFeatureFlags';
import assign from 'shared/assign';
import noop from 'shared/noop';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import isArray from 'shared/isArray';
import {SuspenseException, getSuspendedThenable} from './ReactFizzThenable';
@@ -425,8 +426,6 @@ function defaultErrorHandler(error: mixed) {
return null;
}
function noop(): void {}
function RequestInstance(
this: $FlowFixMe,
resumableState: ResumableState,

View File

@@ -20,6 +20,8 @@ import type {
RejectedThenable,
} from 'shared/ReactTypes';
import noop from 'shared/noop';
export opaque type ThenableState = Array<Thenable<any>>;
// An error that is thrown (e.g. by `use`) to trigger Suspense. If we
@@ -40,8 +42,6 @@ export function createThenableState(): ThenableState {
return [];
}
function noop(): void {}
export function trackUsedThenable<T>(
thenableState: ThenableState,
thenable: Thenable<T>,

View File

@@ -105,6 +105,8 @@ import {resolveOwner, setCurrentOwner} from './flight/ReactFlightCurrentOwner';
import {getOwnerStackByComponentInfoInDev} from 'shared/ReactComponentInfoStack';
import {resetOwnerStackLimit} from 'shared/ReactOwnerStackReset';
import noop from 'shared/noop';
import {
callComponentInDEV,
callLazyInitInDEV,
@@ -443,9 +445,7 @@ function defaultErrorHandler(error: mixed) {
// Don't transform to our wrapper
}
function defaultPostponeHandler(reason: string) {
// Noop
}
const defaultPostponeHandler: (reason: string) => void = noop;
function RequestInstance(
this: $FlowFixMe,
@@ -558,8 +558,6 @@ function RequestInstance(
pingedTasks.push(rootTask);
}
function noop() {}
export function createRequest(
model: ReactClientValue,
bundlerConfig: ClientManifest,

View File

@@ -20,6 +20,8 @@ import type {
RejectedThenable,
} from 'shared/ReactTypes';
import noop from 'shared/noop';
export opaque type ThenableState = Array<Thenable<any>>;
// An error that is thrown (e.g. by `use`) to trigger Suspense. If we
@@ -40,8 +42,6 @@ export function createThenableState(): ThenableState {
return [];
}
function noop(): void {}
export function trackUsedThenable<T>(
thenableState: ThenableState,
thenable: Thenable<T>,

View File

@@ -60,9 +60,9 @@ import {
disableLegacyMode,
} from 'shared/ReactFeatureFlags';
function defaultOnDefaultTransitionIndicator(): void | (() => void) {
// Noop
}
import noop from 'shared/noop';
const defaultOnDefaultTransitionIndicator: () => void | (() => void) = noop;
// $FlowFixMe[prop-missing]: This is only in the development export.
const act = React.act;

View File

@@ -16,6 +16,7 @@ import type {
} from 'shared/ReactTypes';
import isArray from 'shared/isArray';
import noop from 'shared/noop';
import {
getIteratorFn,
REACT_ELEMENT_TYPE,
@@ -82,8 +83,6 @@ function getElementKey(element: any, index: number): string {
return index.toString(36);
}
function noop() {}
function resolveThenable<T>(thenable: Thenable<T>): T {
switch (thenable.status) {
case 'fulfilled': {

View File

@@ -25,6 +25,8 @@ import {
import reportGlobalError from 'shared/reportGlobalError';
import noop from 'shared/noop';
export type Transition = {
types: null | TransitionTypes, // enableViewTransition
gesture: null | GestureProvider, // enableGestureTransition
@@ -177,9 +179,7 @@ export function startGestureTransition(
} finally {
ReactSharedInternals.T = prevTransition;
}
return function cancelGesture() {
// Noop
};
return noop;
}
function warnAboutTransitionSubscriptions(
@@ -200,5 +200,3 @@ function warnAboutTransitionSubscriptions(
}
}
}
function noop() {}

10
packages/shared/noop.js Normal file
View File

@@ -0,0 +1,10 @@
/**
* 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
*/
export default function noop() {}