React components and module exports

我的梦境 提交于 2021-02-06 11:06:13

问题


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 />)
    }})

回答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

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

Then you can use SubComponent directly in main.js



来源:https://stackoverflow.com/questions/35860695/react-components-and-module-exports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!