GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Move createElement/JSX Warnings into the Renderer by sebmarkbage · Pull Request #29088 · react/react

Move createElement/JSX Warnings into the Renderer #29088

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.

Sign up for GitHub

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

Merged
sebmarkbage merged 9 commits into react:main from sebmarkbage:elementstacks
May 23, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

Move createElement/JSX Warnings into the Renderer #29088

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit Hold shift + click to select a range
bde00cc
Move Fragment validation into the reconciler
sebmarkbage May 14, 2024
8086702
Remove extra stack frame addendum indirections
sebmarkbage May 14, 2024
c359809
Don't validate element type in createElement/JSX
sebmarkbage May 14, 2024
90971dc
Don't validate keys in createElement/JSX
sebmarkbage May 14, 2024
8f6c117
Mark child for force warning if assigned implicit key
sebmarkbage May 15, 2024
caa9078
Implement key warning in Fizz
sebmarkbage May 15, 2024
06a041a
Validate key in Flight
sebmarkbage May 16, 2024
047439c
Key warnings are no longer associated with a component in the tree
sebmarkbage May 21, 2024
43ceacd
Fix keys in fixture
sebmarkbage May 21, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
Implement key warning in Fizz
This is just additional for running warnings in the server logs in addition
to the ones that happen during hydration but it's good to have both for
parity and being able to fail SSR tests early.

This also demonstrates per request warning deduping so that they don't just
happen once when the server remains alive.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@sebmarkbage
sebmarkbage committed May 23, 2024
commit caa90781b7ba95f2ecce0788a9283bc04cf4e159

There are no files selected for viewing

10 changes: 9 additions & 1 deletion 10 packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1833,10 +1833,18 @@ describe('ReactDOMFizzServer', () => {
expect(mockError).toHaveBeenCalledWith(
'Warning: Each child in a list should have a unique "key" prop.%s%s' +
' See https://react.dev/link/warning-keys for more information.%s',
'\n\nCheck the top-level render call using <div>.',
gate(flags => flags.enableOwnerStacks)
? // We currently don't track owners in Fizz which is responsible for this frame.
''
: '\n\nCheck the top-level render call using <div>.',
'',
'\n' +
' in span (at **)\n' +
// TODO: Because this validates after the div has been mounted, it is part of
// the parent stack but since owner stacks will switch to owners this goes away again.
(gate(flags => flags.enableOwnerStacks)
? ' in div (at **)\n'
: '') +
' in B (at **)\n' +
' in Suspense (at **)\n' +
' in div (at **)\n' +
Expand Down
2 changes: 1 addition & 1 deletion 2 packages/react-dom/src/__tests__/ReactServerRendering-test.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ describe('ReactDOMServer', () => {
}

function Child() {
return [<A key="1" />, <B key="2" />, <span ariaTypo2="no" />];
return [<A key="1" />, <B key="2" />, <span ariaTypo2="no" key="3" />];
}

function App() {
Expand Down
75 changes: 75 additions & 0 deletions 75 packages/react-server/src/ReactFizzServer.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ export opaque type Request = {
onPostpone: (reason: string, postponeInfo: ThrownInfo) => void,
// Form state that was the result of an MPA submission, if it was provided.
formState: null | ReactFormState<any, any>,
// DEV-only, warning dedupe
didWarnForKey?: null | WeakSet<ComponentStackNode>,
};

// This is a default heuristic for how to split up the HTML content into progressive
Expand Down Expand Up @@ -409,6 +411,9 @@ export function createRequest(
onFatalError: onFatalError === undefined ? noop : onFatalError,
formState: formState === undefined ? null : formState,
};
if (__DEV__) {
request.didWarnForKey = null;
}
// This segment represents the root fallback.
const rootSegment = createPendingSegment(
request,
Expand Down Expand Up @@ -787,6 +792,19 @@ function createClassComponentStack(
};
}

function createComponentStackFromType(
task: Task,
type: Function | string,
): ComponentStackNode {
if (typeof type === 'string') {
return createBuiltInComponentStack(task, type);
}
if (shouldConstruct(type)) {
return createClassComponentStack(task, type);
}
return createFunctionComponentStack(task, type);
}

type ThrownInfo = {
componentStack?: string,
};
Expand Down Expand Up @@ -2597,6 +2615,59 @@ function replayFragment(
}
}

function warnForMissingKey(request: Request, task: Task, child: mixed): void {
if (__DEV__) {
if (
child === null ||
typeof child !== 'object' ||
(child.$$typeof !== REACT_ELEMENT_TYPE &&
child.$$typeof !== REACT_PORTAL_TYPE)
) {
return;
}

if (
!child._store ||
((child._store.validated || child.key != null) &&
child._store.validated !== 2)
) {
return;
}

if (typeof child._store !== 'object') {
throw new Error(
'React Component in warnForMissingKey should have a _store. ' +
'This error is likely caused by a bug in React. Please file an issue.',
);
}

// $FlowFixMe[cannot-write] unable to narrow type from mixed to writable object
child._store.validated = 1;

let didWarnForKey = request.didWarnForKey;
if (didWarnForKey == null) {
didWarnForKey = request.didWarnForKey = new WeakSet();
}
const parentStackFrame = task.componentStack;
if (parentStackFrame === null || didWarnForKey.has(parentStackFrame)) {
// We already warned for other children in this parent.
return;
}
didWarnForKey.add(parentStackFrame);

// We create a fake component stack for the child to log the stack trace from.
const stackFrame = createComponentStackFromType(task, (child: any).type);
task.componentStack = stackFrame;
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.',
'',
'',
);
task.componentStack = stackFrame.parent;
}
}

function renderChildrenArray(
request: Request,
task: Task,
Expand All @@ -2618,6 +2689,7 @@ function renderChildrenArray(
return;
}
}

const prevTreeContext = task.treeContext;
const totalChildren = children.length;

Expand Down Expand Up @@ -2650,6 +2722,9 @@ function renderChildrenArray(

for (let i = 0; i < totalChildren; i++) {
const node = children[i];
if (__DEV__) {
warnForMissingKey(request, task, node);
}
task.treeContext = pushTreeContext(prevTreeContext, totalChildren, i);
// We need to use the non-destructive form so that we can safely pop back
// up and render the sibling if something suspends.
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.