Skip to content

Commit 6069fdd

Browse files
satya164grabbou
authored andcommitted
Show better error message when accessing React APIs on React Native
Summary: **Motivation:** Lots of examples on the web still have the old way to import React APIs from React Native. Also when someone upgrades to latest version of React Native without reading the release notes can get confused. This PR adds getters for `createClass` and `Component` and throws an error with a better error message when they are accessed. ![screenshot_20160614-125622](https://cloud.githubusercontent.com/assets/1174278/16034600/47c70222-3230-11e6-9fe4-1a3493708829.png) **Test plan:** Trying to use `ReactNative.createClass` or `ReactNative.Component` will throw an error with this error message. There's currently a bug in `symbolicateStackTrace` which actually crashes the app after showing the error due to the `stack` being null when updating the stack trace. But that's a separate issue which should be fixed separately. For now, to prevent the crash, we need to add the following before the return statement here - https://github.com/facebook/react-native/blob/master/Libraries/JavaScriptAppEn Closes #8099 Differential Revision: D3430468 Pulled By: javache fbshipit-source-id: c098e51e1f2c276d87eca6da3bd91a457d7840c5
1 parent ccd0181 commit 6069fdd

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule throwOnWrongReactAPI
10+
* @flow
11+
*/
12+
13+
'use strict';
14+
15+
function throwOnWrongReactAPI(key: string) {
16+
throw new Error(
17+
`Seems you're trying to access 'ReactNative.${key}' from the 'react-native' package. Perhaps you meant to access 'React.${key}' from the 'react' package instead?
18+
19+
For example, instead of:
20+
21+
import React, { Component, View } from 'react-native';
22+
23+
You should now do:
24+
25+
import React, { Component } from 'react';
26+
import { View } from 'react-native';
27+
28+
Check the release notes on how to upgrade your code - https://github.com/facebook/react-native/releases/tag/v0.25.1
29+
`);
30+
}
31+
32+
module.exports = throwOnWrongReactAPI;

Libraries/react-native/react-native.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ const ReactNative = {
173173
},
174174
};
175175

176+
// Better error messages when accessing React APIs on ReactNative
177+
if (__DEV__) {
178+
const throwOnWrongReactAPI = require('throwOnWrongReactAPI');
179+
const reactAPIs = [ 'createClass', 'Component' ];
180+
181+
for (const key of reactAPIs) {
182+
Object.defineProperty(ReactNative, key, {
183+
get() { throwOnWrongReactAPI(key); },
184+
enumerable: false,
185+
configurable: false
186+
});
187+
}
188+
}
189+
176190
// Preserve getters with warnings on the internal ReactNative copy without
177191
// invoking them.
178192
const ReactNativeInternal = require('ReactNative');

0 commit comments

Comments
 (0)