Skip to content

Commit ec5638a

Browse files
huntiefacebook-github-bot
authored andcommitted
Expose ReactNativeVersion API (#52784)
Summary: Pull Request resolved: #52784 Resolves react-native-community/discussions-and-proposals#893 (comment). **Changes** - Formalises the design of `ReactNativeVersion` as a single object and adds a `getVersionString` accessor. - Expose `ReactNativeVersion` as a root export on `index.js`. - Update deep imports use in `NewAppScreen`. **Notes** - Subtly, we also have `Platform.constants.reactNativeVersion` in our public API already. **However**, this is the per-platform ***native-reported*** RN version, distinct from the JS version (this diff). See [`ReactNativeVersionCheck.js`](https://github.com/facebook/react-native/blob/54d733311d87e9ab4e18f947edf3f5c85f9a6275/packages/react-native/Libraries/Core/ReactNativeVersionCheck.js#L24). Changelog: [General][Added] - Expose `ReactNativeVersion` API as JavaScript root export Reviewed By: cortinico Differential Revision: D78806347 fbshipit-source-id: 974251fdaa9ab18fac8a584644fea894e4f6e083
1 parent 99edc42 commit ec5638a

File tree

7 files changed

+164
-55
lines changed

7 files changed

+164
-55
lines changed

packages/new-app-screen/src/NewAppScreen.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {ThemedText, useTheme} from './Theme';
1313
import * as React from 'react';
1414
import {
1515
Image,
16+
ReactNativeVersion,
1617
ScrollView,
1718
StyleSheet,
1819
Text,
@@ -22,7 +23,6 @@ import {
2223
useWindowDimensions,
2324
} from 'react-native';
2425
import openURLInBrowser from 'react-native/Libraries/Core/Devtools/openURLInBrowser';
25-
import {version as ReactNativeVersion} from 'react-native/Libraries/Core/ReactNativeVersion';
2626

2727
export type NewAppScreenProps = $ReadOnly<{
2828
templateFileName?: string,
@@ -109,19 +109,9 @@ export default function NewAppScreen({
109109
}
110110

111111
function getVersionLabel(): React.Node {
112-
const version =
113-
[
114-
ReactNativeVersion.major,
115-
ReactNativeVersion.minor,
116-
ReactNativeVersion.patch,
117-
].join('.') +
118-
(ReactNativeVersion.prerelease != null
119-
? '-' + ReactNativeVersion.prerelease
120-
: '');
121-
122112
return (
123113
<ThemedText color="secondary" style={styles.label}>
124-
Version: {version}
114+
Version: {ReactNativeVersion.getVersionString()}
125115
</ThemedText>
126116
);
127117
}

packages/react-native/Libraries/Core/ReactNativeVersion.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,41 @@
99
* @generated by scripts/releases/set-version.js
1010
*/
1111

12-
export const version: $ReadOnly<{
13-
major: number,
14-
minor: number,
15-
patch: number,
16-
prerelease: string | null,
17-
}> = {
18-
major: 1000,
19-
minor: 0,
20-
patch: 0,
21-
prerelease: null,
12+
/**
13+
* Object containing the current React Native version.
14+
*
15+
* Specifically, this is the source of truth for the resolved `react-native`
16+
* package in the JavaScript bundle. Apps and libraries can use this to
17+
* determine compatibility or enable version-specific features.
18+
*
19+
* @example
20+
* ```js
21+
* // Get the full version string
22+
* const version = ReactNativeVersion.getVersionString();
23+
*
24+
* // Access individual version components
25+
* const major = ReactNativeVersion.major;
26+
* ```
27+
*/
28+
export default class ReactNativeVersion {
29+
static major: number = 1000;
30+
static minor: number = 0;
31+
static patch: number = 0;
32+
static prerelease: string | null = null;
33+
34+
static getVersionString(): string {
35+
return `${this.major}.${this.minor}.${this.patch}${this.prerelease != null ? `-${this.prerelease}` : ''}`;
36+
}
37+
}
38+
39+
/**
40+
* @deprecated Compatibility export — please import `ReactNativeVersion` from
41+
* `react-native`.
42+
* See https://github.com/react-native-community/discussions-and-proposals/pull/894.
43+
*/
44+
export const version = {
45+
major: ReactNativeVersion.major,
46+
minor: ReactNativeVersion.minor,
47+
patch: ReactNativeVersion.patch,
48+
prerelease: ReactNativeVersion.prerelease,
2249
};

packages/react-native/ReactNativeApi.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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-
* @generated SignedSource<<a942544e271327a89ec40682d3ec736d>>
7+
* @generated SignedSource<<7839de712363f6691821e0c77dc6b8e8>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -3994,6 +3994,13 @@ declare class ReactNativeElement_default
39943994
get offsetWidth(): number
39953995
setNativeProps(nativeProps: {}): void
39963996
}
3997+
declare class ReactNativeVersion {
3998+
static major: number
3999+
static minor: number
4000+
static patch: number
4001+
static prerelease: null | string
4002+
static getVersionString(): string
4003+
}
39974004
declare class ReadOnlyCharacterData_default extends ReadOnlyNode_default {
39984005
get data(): string
39994006
get length(): number
@@ -6080,6 +6087,7 @@ export {
60806087
PushNotificationIOS, // b4d1fe78
60816088
PushNotificationPermissions, // c2e7ae4f
60826089
Rationale, // 5df1b1c1
6090+
ReactNativeVersion, // abd76827
60836091
RefreshControl, // effd2a00
60846092
RefreshControlProps, // fca5a05f
60856093
RefreshControlPropsAndroid, // 99f64c97

packages/react-native/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ module.exports = {
289289
get requireNativeComponent() {
290290
return require('./Libraries/ReactNative/requireNativeComponent').default;
291291
},
292+
get ReactNativeVersion() {
293+
return require('./Libraries/Core/ReactNativeVersion').default;
294+
},
292295
get RootTagContext() {
293296
return require('./Libraries/ReactNative/RootTag').RootTagContext;
294297
},

packages/react-native/index.js.flow

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ export {default as processColor} from './Libraries/StyleSheet/processColor';
358358
export {default as registerCallableModule} from './Libraries/Core/registerCallableModule';
359359
export {default as requireNativeComponent} from './Libraries/ReactNative/requireNativeComponent';
360360

361+
export {default as ReactNativeVersion} from './Libraries/Core/ReactNativeVersion';
362+
361363
export type {RootTag} from './Libraries/ReactNative/RootTag';
362364
export {RootTagContext} from './Libraries/ReactNative/RootTag';
363365

scripts/releases/__tests__/__snapshots__/set-rn-artifacts-version-test.js.snap

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,43 @@ exports[`updateReactNativeArtifacts should set nightly version: packages/react-n
1212
* << GENERATED >>
1313
*/
1414
15-
export const version: $ReadOnly<{
16-
major: number,
17-
minor: number,
18-
patch: number,
19-
prerelease: string | null,
20-
}> = {
21-
major: 0,
22-
minor: 81,
23-
patch: 0,
24-
prerelease: 'nightly-29282302-abcd1234',
15+
/**
16+
* Object containing the current React Native version.
17+
*
18+
* Specifically, this is the source of truth for the resolved \`react-native\`
19+
* package in the JavaScript bundle. Apps and libraries can use this to
20+
* determine compatibility or enable version-specific features.
21+
*
22+
* @example
23+
* \`\`\`js
24+
* // Get the full version string
25+
* const version = ReactNativeVersion.getVersionString();
26+
*
27+
* // Access individual version components
28+
* const major = ReactNativeVersion.major;
29+
* \`\`\`
30+
*/
31+
export default class ReactNativeVersion {
32+
static major: number = 0;
33+
static minor: number = 81;
34+
static patch: number = 0;
35+
static prerelease: string | null = 'nightly-29282302-abcd1234';
36+
37+
static getVersionString(): string {
38+
return \`\${this.major}.\${this.minor}.\${this.patch}\${this.prerelease != null ? \`-\${this.prerelease}\` : ''}\`;
39+
}
40+
}
41+
42+
/**
43+
* @deprecated Compatibility export — please import \`ReactNativeVersion\` from
44+
* \`react-native\`.
45+
* See https://github.com/react-native-community/discussions-and-proposals/pull/894.
46+
*/
47+
export const version = {
48+
major: ReactNativeVersion.major,
49+
minor: ReactNativeVersion.minor,
50+
patch: ReactNativeVersion.patch,
51+
prerelease: ReactNativeVersion.prerelease,
2552
};
2653
"
2754
`;
@@ -140,16 +167,43 @@ exports[`updateReactNativeArtifacts should set release version: packages/react-n
140167
* << GENERATED >>
141168
*/
142169
143-
export const version: $ReadOnly<{
144-
major: number,
145-
minor: number,
146-
patch: number,
147-
prerelease: string | null,
148-
}> = {
149-
major: 0,
150-
minor: 81,
151-
patch: 0,
152-
prerelease: null,
170+
/**
171+
* Object containing the current React Native version.
172+
*
173+
* Specifically, this is the source of truth for the resolved \`react-native\`
174+
* package in the JavaScript bundle. Apps and libraries can use this to
175+
* determine compatibility or enable version-specific features.
176+
*
177+
* @example
178+
* \`\`\`js
179+
* // Get the full version string
180+
* const version = ReactNativeVersion.getVersionString();
181+
*
182+
* // Access individual version components
183+
* const major = ReactNativeVersion.major;
184+
* \`\`\`
185+
*/
186+
export default class ReactNativeVersion {
187+
static major: number = 0;
188+
static minor: number = 81;
189+
static patch: number = 0;
190+
static prerelease: string | null = null;
191+
192+
static getVersionString(): string {
193+
return \`\${this.major}.\${this.minor}.\${this.patch}\${this.prerelease != null ? \`-\${this.prerelease}\` : ''}\`;
194+
}
195+
}
196+
197+
/**
198+
* @deprecated Compatibility export — please import \`ReactNativeVersion\` from
199+
* \`react-native\`.
200+
* See https://github.com/react-native-community/discussions-and-proposals/pull/894.
201+
*/
202+
export const version = {
203+
major: ReactNativeVersion.major,
204+
minor: ReactNativeVersion.minor,
205+
patch: ReactNativeVersion.patch,
206+
prerelease: ReactNativeVersion.prerelease,
153207
};
154208
"
155209
`;

scripts/releases/templates/ReactNativeVersion.js-template.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,42 @@ module.exports = ({version} /*: {version: Version} */) /*: string */ => `/**
2323
* ${'@'}generated by scripts/releases/set-version.js
2424
*/
2525
26-
export const version: $ReadOnly<{
27-
major: number,
28-
minor: number,
29-
patch: number,
30-
prerelease: string | null,
31-
}> = {
32-
major: ${version.major},
33-
minor: ${version.minor},
34-
patch: ${version.patch},
35-
prerelease: ${
36-
version.prerelease != null ? `'${version.prerelease}'` : 'null'
37-
},
26+
/**
27+
* Object containing the current React Native version.
28+
*
29+
* Specifically, this is the source of truth for the resolved \`react-native\`
30+
* package in the JavaScript bundle. Apps and libraries can use this to
31+
* determine compatibility or enable version-specific features.
32+
*
33+
* @example
34+
* \`\`\`js
35+
* // Get the full version string
36+
* const version = ReactNativeVersion.getVersionString();
37+
*
38+
* // Access individual version components
39+
* const major = ReactNativeVersion.major;
40+
* \`\`\`
41+
*/
42+
export default class ReactNativeVersion {
43+
static major: number = ${version.major};
44+
static minor: number = ${version.minor};
45+
static patch: number = ${version.patch};
46+
static prerelease: string | null = ${version.prerelease != null ? `'${version.prerelease}'` : 'null'};
47+
48+
static getVersionString(): string {
49+
return \`${"${this.major}.${this.minor}.${this.patch}${this.prerelease != null ? `-${this.prerelease}` : ''}"}\`;
50+
}
51+
}
52+
53+
/**
54+
* @deprecated Compatibility export — please import \`ReactNativeVersion\` from
55+
* \`react-native\`.
56+
* See https://github.com/react-native-community/discussions-and-proposals/pull/894.
57+
*/
58+
export const version = {
59+
major: ReactNativeVersion.major,
60+
minor: ReactNativeVersion.minor,
61+
patch: ReactNativeVersion.patch,
62+
prerelease: ReactNativeVersion.prerelease,
3863
};
3964
`;

0 commit comments

Comments
 (0)