2017-02-20 115 views

回答

0

当您的特定平台的代码比较复杂,你应该考虑拆分出来的代码到单独的文件。 React Native将检测文件何时具有.ios..android.扩展名,并在需要时从其他组件加载相关平台文件。

见:https://facebook.github.io/react-native/docs/platform-specific-code.html#platform-specific-extensions

因此,如果您添加或.android.js.ios.js您的扩展RN会选择基于该平台的其中之一。如果你想为这两个平台相同的组件只是不添加特定于平台的延伸和只使用foo.js

0

我想你问什么可以做这种方式:

import { AppRegistry } from 'react-native'; 
import Home from './path/to/home'; 
AppRegistry.registerComponent('SBlank',() => Home); 

通过这样做,你都index.android.jsindex.ios.js您将您的应用重定向到Home.js文件。

奖励:除了knowbody的回答,您可以使用Platform对象来改变你的代码是如何工作在不同的平台。例如:

render() { 
    if (Platform.OS === 'android') { 
     return this.renderAndroid(); 
    } 
    return this.renderIOS(); 
} 

,你加入

import { Platform } from 'react-native'; 
进口平台