javascript - Requiring Modules in react-native -


i'm stuck on problem in react-native project. i'm trying general require file, export modules. after require "require.js" file avoid calls require('../../modulename') in every file.

i have 4 files:

index.ios.js /app/home.js /app/myview.js /app/require.js 

require.js:

module.exports = {      home: require('./home'),     myview: require('./myview')  } 

in index.ios.js (he modules home , myview getting importet correctly)

'use strict';  var react = require('react-native'); var {     appregistry,     stylesheet,     text,     view, } = react;  var {     home,     myview } = require('./app/require');  class test_require extends react.component {      render() {          return(             <home />         );     }  }    appregistry.registercomponent('test_require', () => test_require); 

home.js (the module myview not getting importet)

'use strict';  var react = require('react-native');  var {     view,     text } = react;  var {     myview } = require('./require');  class home extends react.component {      render() {         console.log(myview);         return(             <myview />         );     }  }  module.exports = home; 

in home.js myview variable "undefined". if want require module in module, gets imported in file, variable undefined.

do guys have clue why can or there better solution problem? clue

so i'm posting own answer in case else has same problem.

in syntax required files getting loaded synchronously. if component's build faster requiring files, problem happens. either make components load lazy when need them or use es6 import syntax (import loads asynchronously):

import react 'react-native' 

cheers!


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -