如何在React Native中创建默认页面?
答
当您的特定平台的代码比较复杂,你应该考虑拆分出来的代码到单独的文件。 React Native将检测文件何时具有
.ios.
或.android.
扩展名,并在需要时从其他组件加载相关平台文件。
因此,如果您添加或.android.js
到.ios.js
您的扩展RN会选择基于该平台的其中之一。如果你想为这两个平台相同的组件只是不添加特定于平台的延伸和只使用foo.js
答
我想你问什么可以做这种方式:
import { AppRegistry } from 'react-native';
import Home from './path/to/home';
AppRegistry.registerComponent('SBlank',() => Home);
通过这样做,你都index.android.js
和index.ios.js
您将您的应用重定向到Home.js文件。
奖励:除了knowbody的回答,您可以使用Platform
对象来改变你的代码是如何工作在不同的平台。例如:
render() {
if (Platform.OS === 'android') {
return this.renderAndroid();
}
return this.renderIOS();
}
,你加入
import { Platform } from 'react-native';
进口平台