GeezerVison is showing github.com — Remove _owner field from JSX elements in prod if string refs are disa… · react/react-native@fd0da3e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/jest-react/src/JestReact.js +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | */ | 7 | 7 |
| 8 | 8 | import {REACT_ELEMENT_TYPE, REACT_FRAGMENT_TYPE} from 'shared/ReactSymbols'; | 9 | | -import {enableRefAsProp} from 'shared/ReactFeatureFlags'; | | 9 | +import {disableStringRefs, enableRefAsProp} from 'shared/ReactFeatureFlags'; | 10 | 10 |
| 11 | 11 | import isArray from 'shared/isArray'; | 12 | 12 |
| @@ -54,6 +54,14 @@ function createJSXElementForTestComparison(type, props) { | 54 | 54 | value: null, | 55 | 55 | }); | 56 | 56 | return element; | | 57 | + } else if (!__DEV__ && disableStringRefs) { | | 58 | + return { | | 59 | + $$typeof: REACT_ELEMENT_TYPE, | | 60 | + type: type, | | 61 | + key: null, | | 62 | + ref: null, | | 63 | + props: props, | | 64 | + }; | 57 | 65 | } else { | 58 | 66 | return { | 59 | 67 | $$typeof: REACT_ELEMENT_TYPE, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/ReactFlightClient.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,7 @@ import type {Postpone} from 'react/src/ReactPostpone'; | 38 | 38 | import type {TemporaryReferenceSet} from './ReactFlightTemporaryReferences'; | 39 | 39 |
| 40 | 40 | import { | | 41 | + disableStringRefs, | 41 | 42 | enableBinaryFlight, | 42 | 43 | enablePostpone, | 43 | 44 | enableRefAsProp, | @@ -498,6 +499,16 @@ function createElement( | 498 | 499 | enumerable: false, | 499 | 500 | get: nullRefGetter, | 500 | 501 | }); | | 502 | + } else if (!__DEV__ && disableStringRefs) { | | 503 | + element = ({ | | 504 | + // This tag allows us to uniquely identify this as a React Element | | 505 | + $$typeof: REACT_ELEMENT_TYPE, | | 506 | + | | 507 | + type, | | 508 | + key, | | 509 | + ref: null, | | 510 | + props, | | 511 | + }: any); | 501 | 512 | } else { | 502 | 513 | element = ({ | 503 | 514 | // This tag allows us to uniquely identify this as a React Element | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-noop-renderer/src/createReactNoop.js +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,7 +32,11 @@ import { | 32 | 32 | ConcurrentRoot, | 33 | 33 | LegacyRoot, | 34 | 34 | } from 'react-reconciler/constants'; | 35 | | -import {enableRefAsProp, disableLegacyMode} from 'shared/ReactFeatureFlags'; | | 35 | +import { | | 36 | + enableRefAsProp, | | 37 | + disableLegacyMode, | | 38 | + disableStringRefs, | | 39 | +} from 'shared/ReactFeatureFlags'; | 36 | 40 |
| 37 | 41 | type Container = { | 38 | 42 | rootID: string, | @@ -799,6 +803,14 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { | 799 | 803 | value: null, | 800 | 804 | }); | 801 | 805 | return element; | | 806 | + } else if (!__DEV__ && disableStringRefs) { | | 807 | + return { | | 808 | + $$typeof: REACT_ELEMENT_TYPE, | | 809 | + type: type, | | 810 | + key: null, | | 811 | + ref: null, | | 812 | + props: props, | | 813 | + }; | 802 | 814 | } else { | 803 | 815 | return { | 804 | 816 | $$typeof: REACT_ELEMENT_TYPE, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react/src/__tests__/ReactCreateElement-test.js +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -275,7 +275,11 @@ describe('ReactCreateElement', () => { | 275 | 275 | } | 276 | 276 | const root = ReactDOMClient.createRoot(document.createElement('div')); | 277 | 277 | await act(() => root.render(React.createElement(Wrapper))); | 278 | | - expect(element._owner.stateNode).toBe(instance); | | 278 | + if (__DEV__ || !gate(flags => flags.disableStringRefs)) { | | 279 | + expect(element._owner.stateNode).toBe(instance); | | 280 | + } else { | | 281 | + expect('_owner' in element).toBe(false); | | 282 | + } | 279 | 283 | }); | 280 | 284 |
| 281 | 285 | it('merges an additional argument onto the children prop', () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react/src/jsx/ReactJSXElement.js +15-2Lines changed: 15 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -239,6 +239,19 @@ function ReactElement(type, key, _ref, self, source, owner, props) { | 239 | 239 | value: null, | 240 | 240 | }); | 241 | 241 | } | | 242 | + } else if (!__DEV__ && disableStringRefs) { | | 243 | + // In prod, `ref` is a regular property and _owner doesn't exist. | | 244 | + element = { | | 245 | + // This tag allows us to uniquely identify this as a React Element | | 246 | + $$typeof: REACT_ELEMENT_TYPE, | | 247 | + | | 248 | + // Built-in properties that belong on the element | | 249 | + type, | | 250 | + key, | | 251 | + ref, | | 252 | + | | 253 | + props, | | 254 | + }; | 242 | 255 | } else { | 243 | 256 | // In prod, `ref` is a regular property. It will be removed in a | 244 | 257 | // future release. | @@ -774,7 +787,7 @@ export function cloneAndReplaceKey(oldElement, newKey) { | 774 | 787 | enableRefAsProp ? null : oldElement.ref, | 775 | 788 | undefined, | 776 | 789 | undefined, | 777 | | - oldElement._owner, | | 790 | + !__DEV__ && disableStringRefs ? undefined : oldElement._owner, | 778 | 791 | oldElement.props, | 779 | 792 | ); | 780 | 793 | } | @@ -800,7 +813,7 @@ export function cloneElement(element, config, children) { | 800 | 813 | let ref = enableRefAsProp ? null : element.ref; | 801 | 814 |
| 802 | 815 | // Owner will be preserved, unless ref is overridden | 803 | | - let owner = element._owner; | | 816 | + let owner = !__DEV__ && disableStringRefs ? undefined : element._owner; | 804 | 817 |
| 805 | 818 | if (config != null) { | 806 | 819 | if (hasValidRef(config)) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/ReactElementType.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,7 +13,7 @@ export type ReactElement = { | 13 | 13 | key: any, | 14 | 14 | ref: any, | 15 | 15 | props: any, | 16 | | - // ReactFiber | | 16 | + // __DEV__ or for string refs | 17 | 17 | _owner: any, | 18 | 18 |
| 19 | 19 | // __DEV__ | |
|