Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 exported.

//component.js

var SubComponent = React.createClass({
...
    });

var Component = React.createClass({
    ...
    render: function () { 
    return(
        <div><SubComponent /> stuff</div>`)
        }});

module.exports = Component

//main.js

    var Component = require('./component.js');

    var MainContainer = React.createClass({
    render: function () {return (
    <Component />)
    }})
like image 859
akantoword Avatar asked Mar 08 '16 06:03

akantoword


1 Answers

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

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

Then you can use SubComponent directly in main.js

like image 170
Grzegorz Motyl Avatar answered Nov 06 '22 00:11

Grzegorz Motyl