Skip to content Skip to sidebar Skip to footer

React Components And Module Exports

I don't understand how module.exports can only export one component that has a dependency on a subcomponent but still be rendered in the DOM although that sub component was never e

Solution 1:

You are using only one component directly (Component) in your main.js file. SubComponent is not used outside component.js, therefore it doesn't have to be exported. If you want to use SubComponent in your main.js file you could use it like this:

//component.js

(...)
module.exports = {
    Component: Component,
    SubComponent: SubComponent
}

//main.js

varComponent = require('./component.js').Component;
 varSubComponent = require('./component.js').SubComponent; (...)

Then you can use SubComponent directly in main.js

Post a Comment for "React Components And Module Exports"