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

jsx(): Treat __self and __source as normal props #28257

Merged
merged 1 commit into from
Feb 7, 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
16 changes: 14 additions & 2 deletions packages/react/src/ReactElementProd.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,14 @@ export function createElement(type, config, children) {
hasOwnProperty.call(config, propName) &&
// Skip over reserved prop names
propName !== 'key' &&
// TODO: These will no longer be reserved in the next major
// TODO: `ref` will no longer be reserved in the next major
propName !== 'ref' &&
// ...and maybe these, too, though we currently rely on them for
// warnings and debug information in dev. Need to decide if we're OK
// with dropping them. In the jsx() runtime it's not an issue because
// the data gets passed as separate arguments instead of props, but
// it would be nice to stop relying on them entirely so we can drop
// them from the internal Fiber field.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment could be updated to reflect that we need to keep them for legacy purposes for React.createElement calls but they're not actually used anymore.

propName !== '__self' &&
propName !== '__source'
) {
Expand Down Expand Up @@ -375,8 +381,14 @@ export function cloneElement(element, config, children) {
hasOwnProperty.call(config, propName) &&
// Skip over reserved prop names
propName !== 'key' &&
// TODO: These will no longer be reserved in the next major
// TODO: `ref` will no longer be reserved in the next major
propName !== 'ref' &&
// ...and maybe these, too, though we currently rely on them for
// warnings and debug information in dev. Need to decide if we're OK
// with dropping them. In the jsx() runtime it's not an issue because
// the data gets passed as separate arguments instead of props, but
// it would be nice to stop relying on them entirely so we can drop
// them from the internal Fiber field.
propName !== '__self' &&
propName !== '__source'
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,26 @@ describe('ReactElementValidator', () => {
React.createElement(Lazy);
expect(didCall).toBe(false);
});

it('__self and __source are treated as normal props', async () => {
// These used to be reserved props because the classic React.createElement
// runtime passed this data as props, whereas the jsxDEV() runtime passes
// them as separate arguments.
function Child({__self, __source}) {
return __self + __source;
}

const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
// NOTE: The Babel transform treats the presence of these props as a syntax
// error but theoretically it doesn't have to. Using spread here to
// circumvent the syntax error and demonstrate that the runtime
// doesn't care.
const props = {
__self: 'Hello ',
__source: 'world!',
};
await act(() => root.render(<Child {...props} />));
expect(container.textContent).toBe('Hello world!');
});
});
12 changes: 4 additions & 8 deletions packages/react/src/jsx/ReactJSXElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,8 @@ export function jsx(type, config, maybeKey) {
hasOwnProperty.call(config, propName) &&
// Skip over reserved prop names
propName !== 'key' &&
// TODO: These will no longer be reserved in the next major
propName !== 'ref' &&
propName !== '__self' &&
propName !== '__source'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These really shouldn't have been filtered out from the start since they're passed as separate arguments in this transform.

// TODO: `ref` will no longer be reserved in the next major
propName !== 'ref'
) {
props[propName] = config[propName];
}
Expand Down Expand Up @@ -316,10 +314,8 @@ export function jsxDEV(type, config, maybeKey, source, self) {
hasOwnProperty.call(config, propName) &&
// Skip over reserved prop names
propName !== 'key' &&
// TODO: These will no longer be reserved in the next major
propName !== 'ref' &&
propName !== '__self' &&
propName !== '__source'
// TODO: `ref` will no longer be reserved in the next major
propName !== 'ref'
) {
props[propName] = config[propName];
}
Expand Down