Advantages of having index.ios.js instead of just index.js

后端 未结 6 1204
南旧
南旧 2021-02-04 19:58

New to react-native and used create-react-native-app to make the scaffolding for my first app. It made an App.js which I guess is equivalent to most ap

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 20:27

    index.js will run on both platforms. index.ios.js will only run on iOS devices while index.android.js only runs on Android devices.

    If you have a view that is going to look the same on both devices and any dependencies you have run on both platforms in the same way skip the platform specifier.

    If, however, the view needs to look a bit different on the two platforms(to follow differing design standards on the two platforms perhaps) or you need to use different dependencies on the two platforms then you need to use the specifiers.

    By having some components be simply .js and others be .io.js or .android.js it allows you to consolidate code where possible while still being able to make platform specific choices when needed.

    It should be noted that the platform specifiers can be used on any component, not just index files. (i.e. You can have MyCoolButton.js that will be used on both platforms and MyRegularButton.ios.js and MyRegularButton.android.js` that will each get used on the appropriate platform automatically.)

提交回复
热议问题