简体   繁体   English

React Native:如何结合index.ios.js和index.android.js

[英]React native: how to combine index.ios.js and index.android.js

I am using a panel module Expanding/Collapsing a panel with animations in React Native 我正在使用面板模块在React Native中使用动画扩展/折叠面板

This library contains two .js files 该库包含两个.js文件

index.ios.js: index.ios.js:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */
'use strict';

import React,{AppRegistry,StyleSheet,Text,ScrollView} from 'react-native';
import Panel from './components/Panel';

var Panels = React.createClass({
  render: function() {
    return (
      <ScrollView style={styles.container}>
        <Panel title="A Panel with short content text">
          <Text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</Text>
        </Panel>
        <Panel title="A Panel with long content text">
          <Text>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</Text>
        </Panel>
        <Panel title="Another Panel">
          <Text>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident.</Text>
        </Panel>
      </ScrollView>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex            : 1,
    backgroundColor : '#f4f7f9',
    paddingTop      : 30
  },

});

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

index.android.js index.android.js

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */
'use strict';
var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;
var Panels = React.createClass({
  render: function() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.android.js
        </Text>
        <Text style={styles.instructions}>
          Shake or press menu button for dev menu
        </Text>
      </View>
    );
  }
});
var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});
AppRegistry.registerComponent('Panels', () => Panels);

I want to combine these two files in the one index.js Is there a way I could do this? 我想将这两个文件合并到一个index.js有没有办法做到这一点? I want this library to work on both ios and android devices. 我希望该库在ios和android设备上都能工作。 Any advice or comments would be appreciated thanks in advance! 任何建议或意见将不胜感激,谢谢!

Solution

The one of simple ways is to use index.js only. 一种简单的方法是仅使用index.js You can separate some codes in index.js through Platform 您可以通过平台index.js分离一些代码

Why? 为什么?

Priority of index 索引优先

index.ios.js = index.android.js > index.js index.ios.js = index.android.js> index.js

I guess index.ios.js is the file you want with android also right? 我想index.ios.js也是您想要的android文件吗? It is working with ios and now you want it to be work fine in android also. 它正在与ios一起使用,现在您希望它在android也能正常工作。

It's simple, follow these steps and you are done. 很简单,请按照以下步骤操作,您就完成了。

  1. Delete index.android.js 删除index.android.js
  2. Rename index.ios.js to index.js index.ios.js重命名为index.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM