Skip to content

Commit 5b5cfd7

Browse files
rubennortefacebook-github-bot
authored andcommitted
Improve typing of renderApplication
Summary: General typing improvements to the `renderApplication` module: - Refactors `renderApplication` to take a single options object instead of positional arguments, and removes the unused `fabric` and `_unused` parameters. The `fabric` parameter was already always treated as `true` internally, and the `_unused` slot was a reserved positional placeholder for a removed `scopedPerformanceLogger` argument. - Migrates the file to `flow strict-local`. - Types `RootComponent` and `WrapperComponent` using component syntax instead of `React.ComponentType`. - Replaces `rootTag: any` with `number | RootTag`, normalizing via the existing `createRootTag` helper for `<AppContainer>` and `Number()` for `Renderer.renderElement`. - Simplifies `ActivityType` (drops the unnecessary spread) and tightens null/boolean checks (`debugName != null`, `useOffscreen === true`). Exports a new `RenderApplicationOptions<Props>` type and updates all internal callers accordingly. Changelog: [Internal] Reviewed By: javache Differential Revision: D102325691
1 parent 1e8e182 commit 5b5cfd7

3 files changed

Lines changed: 39 additions & 35 deletions

File tree

packages/react-native/Libraries/ReactNative/AppRegistryImpl.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,21 @@ export function registerComponent(
8383
): string {
8484
runnables[appKey] = (appParameters, displayMode) => {
8585
const renderApplication = require('./renderApplication').default;
86-
renderApplication(
87-
componentProviderInstrumentationHook(
86+
renderApplication({
87+
RootComponent: componentProviderInstrumentationHook(
8888
componentProvider,
8989
DeprecatedPerformanceLoggerStub,
9090
),
91-
appParameters.initialProps,
92-
appParameters.rootTag,
93-
wrapperComponentProvider && wrapperComponentProvider(appParameters),
94-
rootViewStyleProvider && rootViewStyleProvider(appParameters),
95-
true, // fabric - deprecated, always true
96-
undefined, // formerly scopedPerformanceLogger; reserved positional slot
97-
appKey === 'LogBox', // is logbox
98-
appKey,
91+
initialProps: appParameters.initialProps,
92+
rootTag: appParameters.rootTag,
93+
WrapperComponent:
94+
wrapperComponentProvider && wrapperComponentProvider(appParameters),
95+
rootViewStyle:
96+
rootViewStyleProvider && rootViewStyleProvider(appParameters),
97+
isLogBox: appKey === 'LogBox',
98+
debugName: appKey,
9999
displayMode,
100-
);
100+
});
101101
};
102102
if (section) {
103103
sections[appKey] = runnables[appKey];

packages/react-native/Libraries/ReactNative/RendererImplementation.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @format
99
*/
1010

11+
import type {RootTag} from './RootTag';
12+
1113
import {
1214
onCaughtError,
1315
onRecoverableError,
@@ -21,11 +23,11 @@ export function renderElement({
2123
rootTag,
2224
}: {
2325
element: React.MixedElement,
24-
rootTag: number,
26+
rootTag: RootTag,
2527
}): void {
2628
ReactFabric.render(
2729
element,
28-
rootTag,
30+
Number(rootTag),
2931
/* callback */ null,
3032
/* useConcurrentRoot */ true,
3133
{

packages/react-native/Libraries/ReactNative/renderApplication.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,49 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict-local
88
* @format
99
*/
1010

1111
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
12+
import type {RootTag} from './RootTag';
1213

1314
import AppContainer from './AppContainer';
1415
import DisplayMode, {type DisplayModeType} from './DisplayMode';
1516
import getCachedComponentWithDebugName from './getCachedComponentWithDebugName';
1617
import * as Renderer from './RendererProxy';
18+
import {createRootTag} from './RootTag';
1719
import invariant from 'invariant';
1820
import * as React from 'react';
1921

2022
// require BackHandler so it sets the default handler that exits the app if no listeners respond
2123
import '../Utilities/BackHandler';
2224

23-
type ActivityType = component(
24-
...{
25-
mode: 'visible' | 'hidden',
26-
children: React.Node,
27-
}
28-
);
25+
type ActivityType = component(mode: 'visible' | 'hidden', children: React.Node);
2926

30-
export default function renderApplication<Props extends Object>(
31-
RootComponent: React.ComponentType<Props>,
27+
export type RenderApplicationOptions<Props extends {...}> = {
28+
RootComponent: component(...Props),
3229
initialProps: Props,
33-
rootTag: any,
34-
WrapperComponent?: ?React.ComponentType<any>,
30+
rootTag: number | RootTag,
31+
WrapperComponent?: ?component(initialProps: Props, children: React.Node),
3532
rootViewStyle?: ?ViewStyleProp,
36-
// Keep this parameter for backwards compatibility only. It is always treated as
37-
// true internally.
38-
fabric?: true | void,
39-
// Reserved positional slot (formerly `scopedPerformanceLogger`). Preserved so
40-
// existing callers that supply this argument continue to type-check; the
41-
// value is ignored.
42-
_unused?: void,
4333
isLogBox?: boolean,
4434
debugName?: string,
4535
displayMode?: ?DisplayModeType,
4636
useOffscreen?: boolean,
47-
) {
37+
};
38+
39+
export default function renderApplication<Props extends {...}>({
40+
RootComponent,
41+
initialProps,
42+
rootTag,
43+
WrapperComponent,
44+
rootViewStyle,
45+
isLogBox,
46+
debugName,
47+
displayMode,
48+
useOffscreen,
49+
}: RenderApplicationOptions<Props>) {
4850
invariant(rootTag, 'Expect to have a valid rootTag, instead got ', rootTag);
4951

5052
let renderable: React.MixedElement = (
@@ -58,7 +60,7 @@ export default function renderApplication<Props extends Object>(
5860
</AppContainer>
5961
);
6062

61-
if (__DEV__ && debugName) {
63+
if (__DEV__ && debugName != null) {
6264
const RootComponentWithMeaningfulName = getCachedComponentWithDebugName(
6365
`${debugName}(RootComponent)`,
6466
);
@@ -69,7 +71,7 @@ export default function renderApplication<Props extends Object>(
6971
);
7072
}
7173

72-
if (useOffscreen && displayMode != null) {
74+
if (useOffscreen === true && displayMode != null) {
7375
// $FlowFixMe[incompatible-type]
7476
// $FlowFixMe[missing-export]
7577
// $FlowFixMe[prop-missing] `unstable_Activity` is not yet in the React types.
@@ -85,6 +87,6 @@ export default function renderApplication<Props extends Object>(
8587

8688
Renderer.renderElement({
8789
element: renderable,
88-
rootTag,
90+
rootTag: createRootTag(rootTag),
8991
});
9092
}

0 commit comments

Comments
 (0)