React Native + React (for web) seed project

前端 未结 6 1042
长发绾君心
长发绾君心 2020-12-28 17:34

Is it possible to setup a project which has code for both React Native(Mobile app) + React(web), having the code shred between platforms except for the UI part.

Have

6条回答
  •  情书的邮戳
    2020-12-28 18:33

    Jonathan Kaufman has a good article on how to set this up: http://jkaufman.io/react-web-native-codesharing/

    The basic strategy is to have a different entry point (index.js) for each platform (android/ios/web). Then the majority of your non-rendering code can live in a shared app or common folder. You'll still need to segregate your rendering code (i.e. uses of View, div, etc.), though, as that will differ by platform.

    Pay attention to the comments on that article as well, as there's some good discussion on the pitfalls of this approach. Example:

    By sharing a common package.json between native and web, you've glued them together by their common dependencies, the most important one being react. Let's say you upgrade to a version of react-native that depends on >= react@16, but your web app depends on some other library which depends on =< react@15. --timtas

提交回复
热议问题