Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fiber] Replace setCurrentDebugFiberInDEV with runWithFiberInDEV #29221

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions packages/react-reconciler/src/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ import {pushTreeFork} from './ReactFiberTreeContext';
import {createThenableState, trackUsedThenable} from './ReactFiberThenable';
import {readContextDuringReconciliation} from './ReactFiberNewContext';

import {
getCurrentFiber as getCurrentDebugFiberInDEV,
setCurrentFiber as setCurrentDebugFiberInDEV,
} from './ReactCurrentFiber';
import {runWithFiberInDEV} from './ReactCurrentFiber';

// This tracks the thenables that are unwrapped during reconcilation.
let thenableState: ThenableState | null = null;
Expand Down Expand Up @@ -181,15 +178,14 @@ if (__DEV__) {
const fiber = createFiberFromElement((child: any), returnFiber.mode, 0);
fiber.return = returnFiber;

const prevDebugFiber = getCurrentDebugFiberInDEV();
setCurrentDebugFiberInDEV(fiber);
console.error(
'Each child in a list should have a unique "key" prop.' +
'%s%s See https://react.dev/link/warning-keys for more information.',
currentComponentErrorInfo,
childOwnerAppendix,
);
setCurrentDebugFiberInDEV(prevDebugFiber);
runWithFiberInDEV(fiber, () => {
console.error(
'Each child in a list should have a unique "key" prop.' +
'%s%s See https://react.dev/link/warning-keys for more information.',
currentComponentErrorInfo,
childOwnerAppendix,
);
});
};
}

Expand All @@ -212,14 +208,17 @@ function validateFragmentProps(
fiber = createFiberFromElement(element, returnFiber.mode, 0);
fiber.return = returnFiber;
}
const prevDebugFiber = getCurrentDebugFiberInDEV();
setCurrentDebugFiberInDEV(fiber);
console.error(
'Invalid prop `%s` supplied to `React.Fragment`. ' +
'React.Fragment can only have `key` and `children` props.',
runWithFiberInDEV(
fiber,
erroredKey => {
console.error(
'Invalid prop `%s` supplied to `React.Fragment`. ' +
'React.Fragment can only have `key` and `children` props.',
erroredKey,
);
},
key,
);
setCurrentDebugFiberInDEV(prevDebugFiber);
break;
}
}
Expand All @@ -231,10 +230,9 @@ function validateFragmentProps(
fiber = createFiberFromElement(element, returnFiber.mode, 0);
fiber.return = returnFiber;
}
const prevDebugFiber = getCurrentDebugFiberInDEV();
setCurrentDebugFiberInDEV(fiber);
console.error('Invalid attribute `ref` supplied to `React.Fragment`.');
setCurrentDebugFiberInDEV(prevDebugFiber);
runWithFiberInDEV(fiber, () => {
console.error('Invalid attribute `ref` supplied to `React.Fragment`.');
});
}
}
}
Expand Down
34 changes: 20 additions & 14 deletions packages/react-reconciler/src/ReactCurrentFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,29 @@ function getCurrentFiberStackInDev(): string {
return '';
}

export function resetCurrentDebugFiberInDEV() {
if (__DEV__) {
resetCurrentFiber();
}
}

export function setCurrentDebugFiberInDEV(fiber: Fiber | null) {
export function runWithFiberInDEV<A0, A1, A2, A3, A4, T>(
fiber: null | Fiber,
callback: (A0, A1, A2, A3, A4) => T,
arg0: A0,
arg1: A1,
arg2: A2,
arg3: A3,
arg4: A4,
): T {
if (__DEV__) {
const previousFiber = current;
setCurrentFiber(fiber);
try {
return callback(arg0, arg1, arg2, arg3, arg4);
} finally {
current = previousFiber;
}
}
// These errors should never make it into a build so we don't need to encode them in codes.json
// eslint-disable-next-line react-internal/prod-error-codes
throw new Error(
'runWithFiberInDEV should never be called in production. This is a bug in React.',
);
}

export function resetCurrentFiber() {
Expand All @@ -70,13 +83,6 @@ export function setCurrentFiber(fiber: Fiber | null) {
current = fiber;
}

export function getCurrentFiber(): Fiber | null {
if (__DEV__) {
return current;
}
return null;
}

export function setIsRendering(rendering: boolean) {
if (__DEV__) {
isRendering = rendering;
Expand Down
Loading