0

I'm having an issue instantiating react-native to then use its list view.

var ReactNative = require('react-native');

var MyClass = React.createClass({
...
    render: function(){
    ...
    );
    }
}

We have the following inside of react-native-implementation.js

...

  get AccessibilityInfo() { return require('AccessibilityInfo'); },
  get ActivityIndicator() { return require('ActivityIndicator'); },
  get ART() { return require('ReactNativeART'); },
...

It has about 90 of those get statements. When webpack runs, it errors out on every single one of them. Webpack brings up the following error for the first get, and the name corresponds for the rest (AccessibilityInfo, then ActivityIndicator, then ReactNativeART, etc.).

ERROR in C:/Directory/~/react-native/Libraries/react-native/react-native-implementation.js

Module not found: Error: Can't resolve 'AccessibilityInfo' in 'C:\Directory\node_modules\react-native\Libraries\react-native' @ C:/Directory/~/react-native/Libraries/react-native/react-native-implementation.js 31:35-63 @ ./Directory/MyClass.jsx

Where are all of these gets and there corresponding NPM installs coming from? I don't have them installed, but I can't do npm install AccessibilityInfo. So what am I missing?

Furthemore, what is the difference from the following via facebook's github.io:

import { AppRegistry, ListView, Text, View } from 'react-native';

vs

var ReactNative = require('react-native');

I'm assuming that I'm about to pull everything from React-Native instead of just the listview. Is there a better syntax when using var to get ListView? Would I be able do something like the following ( I currently can't test this because of the previous error):?

var {ListView} require('react-native');
4
  • Do you have Babel set up? Also, why are you using Webpack? React Native runs on Facebook's Haste packager, it hasn't supported Webpack for a while. Are you trying to create a React Native project without using the React Native CLI? Commented Apr 12, 2017 at 17:46
  • I do have babel setup. I have other components on my webpage already running. I wanted to add a react-native component to my webpage, or is that a big no-no? Commented Apr 12, 2017 at 17:51
  • I think you're confusing React with React Native. React is a web component framework, React Native is a framework for cross-platform mobile applications. If you want React Native components in the browser, you'll need to use something like react-native-web Commented Apr 12, 2017 at 17:53
  • The failures you are seeing are because a React Native mobile app needs a lot of support code which is usually generated by the CLI. Commented Apr 12, 2017 at 17:54

1 Answer 1

1

You can't just add React Native to a web project and expect it to work. React Native is usually meant to run on a mobile device while supported by native (Java or Objective-C) code (which does the actual rendering). The actual components React Native provides are mostly written in native code.

If you want to use React Native in a web project, you'll need to use some kind of framework that provides web versions of the native components which React Native provides. An example of that would be react-native-web.

As for your second question, the ES6 import

import { ListView, View, Text } from 'react-native';

is equivalent to the following statement using require:

var { ListView, View, Text } = require('react-native');

However, as this statement uses destructuring assignment, it still requires ES2015 - there is no way to do this using just ES5.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This explains why I've been pulling my hair out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.