Files
react-native/scripts/releases/templates/ReactNativeVersion.js-template.js
Alex Hunt ec5638abd0 Expose ReactNativeVersion API (#52784)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/52784

Resolves https://github.com/react-native-community/discussions-and-proposals/discussions/893#discussioncomment-13860767.

**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
2025-07-23 08:28:32 -07:00

65 lines
1.9 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
/*::
import type {Version} from '../utils/version-utils';
*/
module.exports = ({version} /*: {version: Version} */) /*: string */ => `/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @noformat
* ${'@'}generated by scripts/releases/set-version.js
*/
/**
* Object containing the current React Native version.
*
* Specifically, this is the source of truth for the resolved \`react-native\`
* package in the JavaScript bundle. Apps and libraries can use this to
* determine compatibility or enable version-specific features.
*
* @example
* \`\`\`js
* // Get the full version string
* const version = ReactNativeVersion.getVersionString();
*
* // Access individual version components
* const major = ReactNativeVersion.major;
* \`\`\`
*/
export default class ReactNativeVersion {
static major: number = ${version.major};
static minor: number = ${version.minor};
static patch: number = ${version.patch};
static prerelease: string | null = ${version.prerelease != null ? `'${version.prerelease}'` : 'null'};
static getVersionString(): string {
return \`${"${this.major}.${this.minor}.${this.patch}${this.prerelease != null ? `-${this.prerelease}` : ''}"}\`;
}
}
/**
* @deprecated Compatibility export — please import \`ReactNativeVersion\` from
* \`react-native\`.
* See https://github.com/react-native-community/discussions-and-proposals/pull/894.
*/
export const version = {
major: ReactNativeVersion.major,
minor: ReactNativeVersion.minor,
patch: ReactNativeVersion.patch,
prerelease: ReactNativeVersion.prerelease,
};
`;