Add a check in InitializeCore.js that PlatformConstants native module is present

Summary: In some enviroments PlatformConstants native module may not be presented in a project, which results in a call to undefined property and a RedBox

Reviewed By: javache

Differential Revision: D5960879

fbshipit-source-id: 80aecbe2f2a61cb410abd5f0dce8ba855e166991
This commit is contained in:
Alex Dvornikov 2017-10-03 05:42:21 -07:00 committed by Facebook Github Bot
parent 346af557c3
commit 96f23e7416

View File

@ -116,21 +116,24 @@ if (!global.__fbDisableExceptionsManager) {
ErrorUtils.setGlobalHandler(handleError); ErrorUtils.setGlobalHandler(handleError);
} }
const formatVersion = version => const {PlatformConstants} = require('NativeModules');
`${version.major}.${version.minor}.${version.patch}` + if (PlatformConstants) {
(version.prerelease !== null ? `-${version.prerelease}` : ''); const formatVersion = version =>
`${version.major}.${version.minor}.${version.patch}` +
(version.prerelease !== null ? `-${version.prerelease}` : '');
const ReactNativeVersion = require('ReactNativeVersion'); const ReactNativeVersion = require('ReactNativeVersion');
const nativeVersion = require('NativeModules').PlatformConstants.reactNativeVersion; const nativeVersion = PlatformConstants.reactNativeVersion;
if (ReactNativeVersion.version.major !== nativeVersion.major || if (ReactNativeVersion.version.major !== nativeVersion.major ||
ReactNativeVersion.version.minor !== nativeVersion.minor) { ReactNativeVersion.version.minor !== nativeVersion.minor) {
throw new Error( throw new Error(
`React Native version mismatch.\n\nJavaScript version: ${formatVersion(ReactNativeVersion.version)}\n` + `React Native version mismatch.\n\nJavaScript version: ${formatVersion(ReactNativeVersion.version)}\n` +
`Native version: ${formatVersion(nativeVersion)}\n\n` + `Native version: ${formatVersion(nativeVersion)}\n\n` +
'Make sure that you have rebuilt the native code. If the problem persists ' + 'Make sure that you have rebuilt the native code. If the problem persists ' +
'try clearing the watchman and packager caches with `watchman watch-del-all ' + 'try clearing the watchman and packager caches with `watchman watch-del-all ' +
'&& react-native start --reset-cache`.' '&& react-native start --reset-cache`.'
); );
}
} }
// Set up collections // Set up collections