Requiring Modules in react-native

后端 未结 3 1660
天涯浪人
天涯浪人 2021-01-06 11:38

I\'m stuck on a problem in a react-native project. I\'m trying to do a general require file, where I export all my modules. After that I would like to require only my \"requ

相关标签:
3条回答
  • 2021-01-06 11:47

    This is way old, but the record needs to be corrected. Require isn't async. You can tell this easily by inspecting the object returned (not a promise).

    Re-exporting in ES6 is simple.

    For example:

    export * from './types';
    export { default as ApiClient } from './ApiClient';
    
    0 讨论(0)
  • 2021-01-06 11:58

    So I'm posting my own answer in case someone else has the same problem.

    In a syntax like this the required files are getting loaded synchronously. So if the component's build is faster than requiring files, this problem happens. Either make your components load lazy when you need them or use es6 import syntax like this (import loads asynchronously):

    import React from 'react-native'
    

    cheers!

    0 讨论(0)
  • 2021-01-06 12:01

    Add @providesModule moduleName in your file header comment. And you can import using require('moduleName') other place in your project.
    See this issue.
    BTW, how come that such a amazing feature never documented anywhere ?

    0 讨论(0)
提交回复
热议问题