How to use index.js instead of (index.ios.js, index.android.js) in React Native for cross-platform app?

喜欢而已 提交于 2019-12-18 02:18:35

问题


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:


回答1:


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




回答2:


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>
        );
    }
}



回答3:


In iOS AppDelegate.m change this

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

to

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


来源:https://stackoverflow.com/questions/44803681/how-to-use-index-js-instead-of-index-ios-js-index-android-js-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!