Skip to content

Commit 07aa494

Browse files
authored
Remove enableRefAsProp feature flag (#30346)
The flag is fully rolled out.
1 parent 543eb09 commit 07aa494

29 files changed

+113
-859
lines changed

Diff for: packages/jest-react/src/JestReact.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import {REACT_ELEMENT_TYPE, REACT_FRAGMENT_TYPE} from 'shared/ReactSymbols';
9-
import {disableStringRefs, enableRefAsProp} from 'shared/ReactFeatureFlags';
9+
import {disableStringRefs} from 'shared/ReactFeatureFlags';
1010
const {assertConsoleLogsCleared} = require('internal-test-utils/consoleMock');
1111

1212
import isArray from 'shared/isArray';
@@ -42,7 +42,7 @@ function assertYieldsWereCleared(root) {
4242
}
4343

4444
function createJSXElementForTestComparison(type, props) {
45-
if (__DEV__ && enableRefAsProp) {
45+
if (__DEV__) {
4646
const element = {
4747
$$typeof: REACT_ELEMENT_TYPE,
4848
type: type,

Diff for: packages/react-client/src/ReactFlightClient.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import {
4444
disableStringRefs,
4545
enableBinaryFlight,
4646
enablePostpone,
47-
enableRefAsProp,
4847
enableFlightReadableStream,
4948
enableOwnerStacks,
5049
enableServerComponentLogs,
@@ -676,7 +675,7 @@ function createElement(
676675
| React$Element<any>
677676
| LazyComponent<React$Element<any>, SomeChunk<React$Element<any>>> {
678677
let element: any;
679-
if (__DEV__ && enableRefAsProp) {
678+
if (__DEV__) {
680679
// `ref` is non-enumerable in dev
681680
element = ({
682681
$$typeof: REACT_ELEMENT_TYPE,

Diff for: packages/react-devtools-shared/src/__tests__/legacy/storeLegacy-v15-test.js

-83
Original file line numberDiff line numberDiff line change
@@ -868,89 +868,6 @@ describe('Store (legacy)', () => {
868868
`);
869869
});
870870

871-
// TODO: These tests don't work when enableRefAsProp is on because the
872-
// JSX runtime that's injected into the test environment by the compiler
873-
// is not compatible with older versions of React. Need to configure the
874-
// the test environment in such a way that certain test modules like this
875-
// one can use an older transform.
876-
if (!require('shared/ReactFeatureFlags').enableRefAsProp) {
877-
it('should support expanding deep parts of the tree', () => {
878-
const Wrapper = ({forwardedRef}) =>
879-
React.createElement(Nested, {
880-
depth: 3,
881-
forwardedRef: forwardedRef,
882-
});
883-
const Nested = ({depth, forwardedRef}) =>
884-
depth > 0
885-
? React.createElement(Nested, {
886-
depth: depth - 1,
887-
forwardedRef: forwardedRef,
888-
})
889-
: React.createElement('div', {
890-
ref: forwardedRef,
891-
});
892-
let ref = null;
893-
const refSetter = value => {
894-
ref = value;
895-
};
896-
act(() =>
897-
ReactDOM.render(
898-
React.createElement(Wrapper, {
899-
forwardedRef: refSetter,
900-
}),
901-
document.createElement('div'),
902-
),
903-
);
904-
expect(store).toMatchInlineSnapshot(`
905-
[root]
906-
▸ <Wrapper>
907-
`);
908-
const deepestedNodeID = global.agent.getIDForHostInstance(ref);
909-
act(() => store.toggleIsCollapsed(deepestedNodeID, false));
910-
expect(store).toMatchInlineSnapshot(`
911-
[root]
912-
▾ <Wrapper>
913-
▾ <Nested>
914-
▾ <Nested>
915-
▾ <Nested>
916-
▾ <Nested>
917-
<div>
918-
`);
919-
const rootID = store.getElementIDAtIndex(0);
920-
act(() => store.toggleIsCollapsed(rootID, true));
921-
expect(store).toMatchInlineSnapshot(`
922-
[root]
923-
▸ <Wrapper>
924-
`);
925-
act(() => store.toggleIsCollapsed(rootID, false));
926-
expect(store).toMatchInlineSnapshot(`
927-
[root]
928-
▾ <Wrapper>
929-
▾ <Nested>
930-
▾ <Nested>
931-
▾ <Nested>
932-
▾ <Nested>
933-
<div>
934-
`);
935-
const id = store.getElementIDAtIndex(1);
936-
act(() => store.toggleIsCollapsed(id, true));
937-
expect(store).toMatchInlineSnapshot(`
938-
[root]
939-
▾ <Wrapper>
940-
▸ <Nested>
941-
`);
942-
act(() => store.toggleIsCollapsed(id, false));
943-
expect(store).toMatchInlineSnapshot(`
944-
[root]
945-
▾ <Wrapper>
946-
▾ <Nested>
947-
▾ <Nested>
948-
▾ <Nested>
949-
▾ <Nested>
950-
<div>
951-
`);
952-
});
953-
}
954871
it('should support reordering of children', () => {
955872
const Root = ({children}) => React.createElement('div', null, children);
956873
const Component = () => React.createElement('div', null);

Diff for: packages/react-dom/src/__tests__/ReactFunctionComponent-test.js

-217
Original file line numberDiff line numberDiff line change
@@ -197,223 +197,6 @@ describe('ReactFunctionComponent', () => {
197197
.rejects.toThrowError();
198198
});
199199

200-
// @gate !enableRefAsProp || !__DEV__
201-
it('should warn when given a string ref', async () => {
202-
function Indirection(props) {
203-
return <div>{props.children}</div>;
204-
}
205-
206-
class ParentUsingStringRef extends React.Component {
207-
render() {
208-
return (
209-
<Indirection>
210-
<FunctionComponent name="A" ref="stateless" />
211-
</Indirection>
212-
);
213-
}
214-
}
215-
216-
await expect(async () => {
217-
const container = document.createElement('div');
218-
const root = ReactDOMClient.createRoot(container);
219-
await act(() => {
220-
root.render(<ParentUsingStringRef />);
221-
});
222-
}).toErrorDev(
223-
'Function components cannot be given refs. ' +
224-
'Attempts to access this ref will fail. ' +
225-
'Did you mean to use React.forwardRef()?\n\n' +
226-
'Check the render method ' +
227-
'of `ParentUsingStringRef`.\n' +
228-
' in FunctionComponent (at **)\n' +
229-
' in div (at **)\n' +
230-
' in Indirection (at **)\n' +
231-
' in ParentUsingStringRef (at **)',
232-
);
233-
234-
// No additional warnings should be logged
235-
const container = document.createElement('div');
236-
const root = ReactDOMClient.createRoot(container);
237-
await act(() => {
238-
root.render(<ParentUsingStringRef />);
239-
});
240-
});
241-
242-
// @gate !enableRefAsProp || !__DEV__
243-
it('should warn when given a function ref', async () => {
244-
function Indirection(props) {
245-
return <div>{props.children}</div>;
246-
}
247-
248-
const ref = jest.fn();
249-
class ParentUsingFunctionRef extends React.Component {
250-
render() {
251-
return (
252-
<Indirection>
253-
<FunctionComponent name="A" ref={ref} />
254-
</Indirection>
255-
);
256-
}
257-
}
258-
259-
await expect(async () => {
260-
const container = document.createElement('div');
261-
const root = ReactDOMClient.createRoot(container);
262-
await act(() => {
263-
root.render(<ParentUsingFunctionRef />);
264-
});
265-
}).toErrorDev(
266-
'Function components cannot be given refs. ' +
267-
'Attempts to access this ref will fail. ' +
268-
'Did you mean to use React.forwardRef()?\n\n' +
269-
'Check the render method ' +
270-
'of `ParentUsingFunctionRef`.\n' +
271-
' in FunctionComponent (at **)\n' +
272-
' in div (at **)\n' +
273-
' in Indirection (at **)\n' +
274-
' in ParentUsingFunctionRef (at **)',
275-
);
276-
expect(ref).not.toHaveBeenCalled();
277-
278-
// No additional warnings should be logged
279-
const container = document.createElement('div');
280-
const root = ReactDOMClient.createRoot(container);
281-
await act(() => {
282-
root.render(<ParentUsingFunctionRef />);
283-
});
284-
});
285-
286-
// @gate !enableRefAsProp || !__DEV__
287-
it('deduplicates ref warnings based on element or owner', async () => {
288-
// When owner uses JSX, we can use exact line location to dedupe warnings
289-
class AnonymousParentUsingJSX extends React.Component {
290-
render() {
291-
return <FunctionComponent name="A" ref={() => {}} />;
292-
}
293-
}
294-
295-
let instance1;
296-
297-
await expect(async () => {
298-
const container = document.createElement('div');
299-
const root = ReactDOMClient.createRoot(container);
300-
301-
await act(() => {
302-
root.render(
303-
<AnonymousParentUsingJSX ref={current => (instance1 = current)} />,
304-
);
305-
});
306-
}).toErrorDev('Function components cannot be given refs.');
307-
// Should be deduped (offending element is on the same line):
308-
instance1.forceUpdate();
309-
// Should also be deduped (offending element is on the same line):
310-
let container = document.createElement('div');
311-
let root = ReactDOMClient.createRoot(container);
312-
await act(() => {
313-
root.render(<AnonymousParentUsingJSX />);
314-
});
315-
316-
// When owner doesn't use JSX, and is anonymous, we warn once per internal instance.
317-
class AnonymousParentNotUsingJSX extends React.Component {
318-
render() {
319-
return React.createElement(FunctionComponent, {
320-
name: 'A',
321-
ref: () => {},
322-
});
323-
}
324-
}
325-
326-
let instance2;
327-
await expect(async () => {
328-
container = document.createElement('div');
329-
root = ReactDOMClient.createRoot(container);
330-
await act(() => {
331-
root.render(
332-
<AnonymousParentNotUsingJSX ref={current => (instance2 = current)} />,
333-
);
334-
});
335-
}).toErrorDev('Function components cannot be given refs.');
336-
// Should be deduped (same internal instance, no additional warnings)
337-
instance2.forceUpdate();
338-
// Could not be differentiated (since owner is anonymous and no source location)
339-
container = document.createElement('div');
340-
root = ReactDOMClient.createRoot(container);
341-
await act(() => {
342-
root.render(<AnonymousParentNotUsingJSX />);
343-
});
344-
345-
// When owner doesn't use JSX, but is named, we warn once per owner name
346-
class NamedParentNotUsingJSX extends React.Component {
347-
render() {
348-
return React.createElement(FunctionComponent, {
349-
name: 'A',
350-
ref: () => {},
351-
});
352-
}
353-
}
354-
let instance3;
355-
await expect(async () => {
356-
container = document.createElement('div');
357-
root = ReactDOMClient.createRoot(container);
358-
await act(() => {
359-
root.render(
360-
<NamedParentNotUsingJSX ref={current => (instance3 = current)} />,
361-
);
362-
});
363-
}).toErrorDev('Function components cannot be given refs.');
364-
// Should be deduped (same owner name, no additional warnings):
365-
instance3.forceUpdate();
366-
// Should also be deduped (same owner name, no additional warnings):
367-
container = document.createElement('div');
368-
root = ReactDOMClient.createRoot(container);
369-
await act(() => {
370-
root.render(<NamedParentNotUsingJSX />);
371-
});
372-
});
373-
374-
// This guards against a regression caused by clearing the current debug fiber.
375-
// https://github.com/facebook/react/issues/10831
376-
// @gate !disableLegacyContext || !__DEV__
377-
// @gate !enableRefAsProp || !__DEV__
378-
it('should warn when giving a function ref with context', async () => {
379-
function Child() {
380-
return null;
381-
}
382-
Child.contextTypes = {
383-
foo: PropTypes.string,
384-
};
385-
386-
class Parent extends React.Component {
387-
static childContextTypes = {
388-
foo: PropTypes.string,
389-
};
390-
getChildContext() {
391-
return {
392-
foo: 'bar',
393-
};
394-
}
395-
render() {
396-
return <Child ref={function () {}} />;
397-
}
398-
}
399-
400-
await expect(async () => {
401-
const container = document.createElement('div');
402-
const root = ReactDOMClient.createRoot(container);
403-
await act(() => {
404-
root.render(<Parent />);
405-
});
406-
}).toErrorDev(
407-
'Function components cannot be given refs. ' +
408-
'Attempts to access this ref will fail. ' +
409-
'Did you mean to use React.forwardRef()?\n\n' +
410-
'Check the render method ' +
411-
'of `Parent`.\n' +
412-
' in Child (at **)\n' +
413-
' in Parent (at **)',
414-
);
415-
});
416-
417200
it('should use correct name in key warning', async () => {
418201
function Child() {
419202
return <div>{[<span />]}</div>;

Diff for: packages/react-dom/src/__tests__/refs-test.js

-18
Original file line numberDiff line numberDiff line change
@@ -369,24 +369,6 @@ describe('ref swapping', () => {
369369
});
370370
}).rejects.toThrow('Expected ref to be a function');
371371
});
372-
373-
// @gate !enableRefAsProp && www
374-
it('undefined ref on manually inlined React element triggers error', async () => {
375-
const container = document.createElement('div');
376-
const root = ReactDOMClient.createRoot(container);
377-
await expect(async () => {
378-
await act(() => {
379-
root.render({
380-
$$typeof: Symbol.for('react.element'),
381-
type: 'div',
382-
props: {
383-
ref: undefined,
384-
},
385-
key: null,
386-
});
387-
});
388-
}).rejects.toThrow('Expected ref to be a function');
389-
});
390372
});
391373

392374
describe('root level refs', () => {

Diff for: packages/react-noop-renderer/src/createReactNoop.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ import {
3535
ConcurrentRoot,
3636
LegacyRoot,
3737
} from 'react-reconciler/constants';
38-
import {
39-
enableRefAsProp,
40-
disableLegacyMode,
41-
disableStringRefs,
42-
} from 'shared/ReactFeatureFlags';
38+
import {disableLegacyMode, disableStringRefs} from 'shared/ReactFeatureFlags';
4339

4440
import ReactSharedInternals from 'shared/ReactSharedInternals';
4541
import ReactVersion from 'shared/ReactVersion';
@@ -833,7 +829,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
833829
let currentEventPriority = DefaultEventPriority;
834830

835831
function createJSXElementForTestComparison(type, props) {
836-
if (__DEV__ && enableRefAsProp) {
832+
if (__DEV__) {
837833
const element = {
838834
type: type,
839835
$$typeof: REACT_ELEMENT_TYPE,

0 commit comments

Comments
 (0)