19

Thanks for the answers from now,

I am a newbie in React Native, I want to make a cross-platform app so I created index.js:

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

class ReactApp extends Component {

    render() {
        return (
            <View><Text>Hello world</Text></View>
        );
    }
}

module.exports = ReactApp;

Then I imported index.js from both index.ios.js and index.android.js like this:

import { AppRegistry } from 'react-native';
import ReactApp from './index';

AppRegistry.registerComponent('ReactApp', () => ReactApp);

I think after this it should work but I get this error:

enter image description here

10
  • 1
    please rename index file and try again Commented Jun 28, 2017 at 13:30
  • Your image is not working. Also, you didn't state what you were trying to accomplish by doing this. Commented Jun 28, 2017 at 13:30
  • @MichaelCheng image is working I think cause I tried on my phone also, I want to make a cross-platform app Commented Jun 28, 2017 at 13:36
  • @ravi.p like how? and why? Commented Jun 28, 2017 at 13:39
  • 1
    replace first line in index.js with this and try import React, {Component} from 'react'; Commented Jun 28, 2017 at 14:01

3 Answers 3

43

After React v0.49, you don't need index.ios.js and index.android.js. You only need the index.js:

import {AppRegistry} from 'react-native';
import App from './app/App';

AppRegistry.registerComponent('appMobile', () => App);

(replace appMobile with the name of your app)

Source: (https://github.com/facebook/react-native/releases/tag/v0.49.0)

New projects have a single entry-point (index.js) from now on

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

6 Comments

That was about I was looking for, because I've worked with other version (older) and I saw the 2 different files, but from 0.49 you will get one file after create the app.
If you want to upgrade your RN version check facebook.github.io/react-native/docs/upgrading.html (react-native-git-upgrade should work like a charme)
is it possible to still use 2 files with new react-native?
I don't think so
The issue explaining something: github.com/facebook/react-native/issues/…
|
7

You're going about this backwards. index.ios.js and index.android.js will always be separate entry points in a default react-native init project. If you want to have them run the same codebase via index.js, you should set it up so index.ios.js and index.android.js import index.js and registers the same base component as defined in index.js.

For example, you can look at how it's done in this example ToDo app (Github repo here).

If you place index.js in your root folder, it will conflict with index.android.js and index.ios.js when you don't reference it directly. So you will need to point to the exact path in your import. See the code below.

index.ios.js and index.android.js (same contents)

import React from 'react';
import { AppRegistry } from 'react-native'
import ReactApp from './index.js'
// Had you placed index.js in another folder like `./app`, you could instead do your import with this shorthand:
// import ReactApp from './app'

AppRegistry.registerComponent('ReactApp', () => ReactApp)

index.js

// Note: I noticed that you imported Component from the wrong place. That might also be contributing to your issue so I fixed it here.
import React, { Component } from 'react';
import {
    View,
    Text,
} from 'react-native';

// Unless you are exporting multiple things from a single file, you should just use this.
// It's more idiomatic than using module.exports = ReactApp;
export default class ReactApp extends Component {
    render() {
        return (
            <View><Text>Hello world</Text></View>
        );
    }
}

2 Comments

I copied and pasted your code but I am getting the same error :-/ ,
@davutdev Try it now with the edits. I missed the React import since I put the answer together really quickly. You still need that in all three files. Also, realized that the shorthand ./index import will have a naming conflict if you place index.js in the same directory as other files starting with index.something. Had you used another filename or placed it elsewhere, that wouldn't have been an issue. I added comments to explain it in the answer. I also tested this in a brand new project just now so if there are any errors still, it must be from something else.
6

In iOS AppDelegate.m change this

jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

to

jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

Comments

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.