Skip to content

Commit 45804af

Browse files
authored
[flow] Eliminate usage of more than 1-arg React.AbstractComponent in React codebase (#31314)
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory. Before submitting a pull request, please make sure the following is done: 1. Fork [the repository](https://github.com/facebook/react) and create your branch from `main`. 2. Run `yarn` in the repository root. 3. If you've fixed a bug or added code that should be tested, add tests! 4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch TestName` is helpful in development. 5. Run `yarn test --prod` to test in the production environment. It supports the same options as `yarn test`. 6. If you need a debugger, run `yarn test --debug --watch TestName`, open `chrome://inspect`, and press "Inspect". 7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`). 8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files. 9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`). 10. If you haven't already, complete the CLA. Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html --> ## Summary In order to adopt react 19's ref-as-prop model, Flow needs to eliminate all the places where they are treated differently. `React.AbstractComponent` is the worst example of this, and we need to eliminate it. This PR eliminates them from the react repo, and only keeps the one that has 1 argument of props. ## How did you test this change? yarn flow
1 parent 69d4b80 commit 45804af

File tree

15 files changed

+36
-38
lines changed

15 files changed

+36
-38
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ module.exports = {
569569
React$Node: 'readonly',
570570
React$Portal: 'readonly',
571571
React$Ref: 'readonly',
572+
React$RefSetter: 'readonly',
572573
ReadableStreamController: 'readonly',
573574
ReadableStreamReader: 'readonly',
574575
RequestInfo: 'readonly',

packages/react-devtools-inline/src/frontend.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function initialize(
5252
bridge?: FrontendBridge,
5353
store?: Store,
5454
} = {},
55-
): React.AbstractComponent<Props, mixed> {
55+
): React.ComponentType<Props> {
5656
if (bridge == null) {
5757
bridge = createBridge(contentWindow);
5858
}

packages/react-devtools-shared/src/devtools/ContextMenu/types.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @flow
88
*/
99

10-
import type {Node as ReactNode, AbstractComponent, ElementRef} from 'react';
10+
import type {Node as ReactNode} from 'react';
1111

1212
export type ContextMenuItem = {
1313
onClick: () => void,
@@ -25,5 +25,7 @@ export type ContextMenuHandle = {
2525
hide(): void,
2626
};
2727

28-
export type ContextMenuComponent = AbstractComponent<{}, ContextMenuHandle>;
29-
export type ContextMenuRef = {current: ElementRef<ContextMenuComponent> | null};
28+
/*::
29+
export type ContextMenuComponent = component(ref: React$RefSetter<ContextMenuHandle>);
30+
*/
31+
export type ContextMenuRef = {current: ContextMenuHandle | null};

packages/react-devtools-shared/src/devtools/views/Components/Components.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,4 @@ function setResizeCSSVariable(
246246
}
247247
}
248248

249-
export default (portaledContent(Components): React$AbstractComponent<{}>);
249+
export default (portaledContent(Components): React$ComponentType<{}>);

packages/react-devtools-shared/src/devtools/views/portaledContent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import ThemeProvider from './ThemeProvider';
1717
export type Props = {portalContainer?: Element, ...};
1818

1919
export default function portaledContent(
20-
Component: React$AbstractComponent<any>,
21-
): React$AbstractComponent<any> {
20+
Component: React$ComponentType<any>,
21+
): React$ComponentType<any> {
2222
return function PortaledContent({portalContainer, ...rest}: Props) {
2323
const store = useContext(StoreContext);
2424

packages/react-devtools-shell/src/app/InspectableElements/CustomHooks.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function useDeepHookF() {
7272
const ContextA = createContext('A');
7373
const ContextB = createContext('B');
7474

75-
function FunctionWithHooks(props: any, ref: React$Ref<any>) {
75+
function FunctionWithHooks(props: any, ref: React$RefSetter<any>) {
7676
const [count, updateCount] = useState(0);
7777
// eslint-disable-next-line no-unused-vars
7878
const contextValueA = useContext(ContextA);
@@ -108,7 +108,9 @@ function FunctionWithHooks(props: any, ref: React$Ref<any>) {
108108
const MemoWithHooks = memo(FunctionWithHooks);
109109
const ForwardRefWithHooks = forwardRef(FunctionWithHooks);
110110

111-
function wrapWithHoc(Component: (props: any, ref: React$Ref<any>) => any) {
111+
function wrapWithHoc(
112+
Component: (props: any, ref: React$RefSetter<any>) => any,
113+
) {
112114
function Hoc() {
113115
return <Component />;
114116
}

packages/react-markup/src/ReactMarkupServer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import {
4646
type ReactMarkupNodeList =
4747
// This is the intersection of ReactNodeList and ReactClientValue minus
4848
// Client/ServerReferences.
49-
| React$Element<React$AbstractComponent<any, any>>
49+
| React$Element<React$ComponentType<any>>
5050
| LazyComponent<ReactMarkupNodeList, any>
5151
| React$Element<string>
5252
| string

packages/react-native-renderer/src/ReactNativeFiberHostComponent.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
* @flow
88
*/
99

10-
import type {ElementRef} from 'react';
1110
import type {
12-
HostComponent,
11+
HostInstance,
1312
MeasureInWindowOnSuccessCallback,
1413
MeasureLayoutOnSuccessCallback,
1514
MeasureOnSuccessCallback,
@@ -72,7 +71,7 @@ class ReactNativeFiberHostComponent implements INativeMethods {
7271
}
7372

7473
measureLayout(
75-
relativeToNativeNode: number | ElementRef<HostComponent<mixed>>,
74+
relativeToNativeNode: number | HostInstance,
7675
onSuccess: MeasureLayoutOnSuccessCallback,
7776
onFail?: () => void /* currently unused */,
7877
) {

packages/react-native-renderer/src/ReactNativePublicCompat.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232

3333
export function findHostInstance_DEPRECATED<TElementType: ElementType>(
3434
componentOrHandle: ?(ElementRef<TElementType> | number),
35-
): ?ElementRef<HostComponent<mixed>> {
35+
): ?ElementRef<HostComponent<{...}>> {
3636
if (__DEV__) {
3737
const owner = currentOwner;
3838
if (owner !== null && isRendering && owner.stateNode !== null) {
@@ -225,7 +225,7 @@ export function getNodeFromInternalInstanceHandle(
225225
// Should have been PublicInstance from ReactFiberConfigFabric
226226
type FabricPublicInstance = mixed;
227227
// Should have been PublicInstance from ReactFiberConfigNative
228-
type PaperPublicInstance = HostComponent<mixed>;
228+
type PaperPublicInstance = HostComponent<empty>;
229229

230230
// Remove this once Paper is no longer supported and DOM Node API are enabled by default in RN.
231231
export function isChildPublicInstance(

packages/react-native-renderer/src/ReactNativeTypes.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99
* @flow strict
1010
*/
1111

12-
import type {
13-
ElementRef,
14-
ElementType,
15-
MixedElement,
16-
AbstractComponent,
17-
} from 'react';
12+
import type {ElementRef, ElementType, MixedElement} from 'react';
1813

1914
export type MeasureOnSuccessCallback = (
2015
x: number,
@@ -137,7 +132,9 @@ declare const ensureNativeMethodsAreSynced: NativeMethods;
137132
(ensureNativeMethodsAreSynced: INativeMethods);
138133

139134
export type HostInstance = NativeMethods;
140-
export type HostComponent<Config> = AbstractComponent<Config, HostInstance>;
135+
/*::
136+
export type HostComponent<Config: {...}> = component(ref: React$RefSetter<HostInstance>, ...Config);
137+
*/
141138

142139
type InspectorDataProps = $ReadOnly<{
143140
[propName: string]: string,
@@ -208,8 +205,10 @@ export type ReactNativeType = {
208205
componentOrHandle: ?(ElementRef<TElementType> | number),
209206
): ?number,
210207
isChildPublicInstance(
211-
parent: PublicInstance | HostComponent<mixed>,
212-
child: PublicInstance | HostComponent<mixed>,
208+
// eslint-disable-next-line no-undef
209+
parent: PublicInstance | HostComponent<empty>,
210+
// eslint-disable-next-line no-undef
211+
child: PublicInstance | HostComponent<empty>,
213212
): boolean,
214213
dispatchCommand(
215214
handle: HostInstance,

0 commit comments

Comments
 (0)