2017-02-20 112 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'; 
進口平臺