最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React components and module exports - Stack Overflow

programmeradmin3浏览0评论

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

//ponent.js

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

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

module.exports = Component

//main.js

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

    var MainContainer = React.createClass({
    render: function () {return (
    <Component />)
    }})

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

//ponent.js

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

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

module.exports = Component

//main.js

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

    var MainContainer = React.createClass({
    render: function () {return (
    <Component />)
    }})
Share Improve this question asked Mar 8, 2016 at 6:39 akantowordakantoword 3,0549 gold badges28 silver badges49 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You are using only one ponent directly (Component) in your main.js file. SubComponent is not used outside ponent.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:

//ponent.js

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

//main.js

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

Then you can use SubComponent directly in main.js

发布评论

评论列表(0)

  1. 暂无评论