3

Is that possible to create an hybrid package for React and React-Native that requires specific import, for instance using React-Native I'll have to import AsyncStorage but I'll not need this one for React (using localStorage).

So I was wondering if that's feasible to handle both case using conditional detecting if the app is React-Native or not.

awzx
  • 1,023
  • 2
  • 12
  • 31

2 Answers2

0

In order to detect if the app is React-Native or not, you will need to use platform specific logic. According to this commit, you should be able to check via:

navigator.product === 'ReactNative'

Also see here for specific platforms: https://facebook.github.io/react-native/docs/platform-specific-code.html

Scott
  • 3,736
  • 2
  • 26
  • 44
  • 1
    I tested it, works well, but I'm still stuck with imports of library in case of its react-native or not. – awzx Jan 01 '17 at 00:16
  • Dynamic imports are done via a `require`. See here: http://stackoverflow.com/questions/35914712/es6-conditional-dynamic-import-statements – Scott Jan 02 '17 at 01:07
  • Actually it has been solved by injecting the global variables of the libraries in the context. – awzx Jan 12 '17 at 10:53
0

For those who can be interested in the future by the implementation of a library that needs specific imports for React and specific imports for React-Native, but can't be mixed, you'll need to create a store for your library, if it can be initialized with React code, you can initialize in your code and create a setter function that'll create a new store to inject the specific dependencies.

awzx
  • 1,023
  • 2
  • 12
  • 31