Modules requirement in

I am stuck in a problem in a real project. I am trying to make a common required file where I export all my modules. After that, I would like to require only my file “require.js” to avoid the calls that are required (“../../ModuleName”) in each 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 (its modules Home and MyView get import 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 (MyView module does not receive import)

'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, the MyView variable is set to "undefined". If I want to require a module in a module that is already being imported into another file, the variable is undefined.

Do you have any clues why I can do this or is there a better solution to my problem? Thanks for any hint.

+4
2

, , - .

. , , , . , , , es6, ( ):

import React from 'react-native'

!

+5

@providesModule moduleName . , require ('moduleName') .
issue.
, , ?

+2

Source: https://habr.com/ru/post/1611725/


All Articles