如何从包含多个组件的文件中要求react组件?

时间:2016-06-08 23:00:10

标签: testing reactjs export

我有以下

 var comp1 = React.createClass({
        render: function() {
            return (
            <div>
            <div class="content">
            <p> information here ..</p>
            </div>
            </div>
            );
        }
    });

    var comp2 = React.createClass({
        render: function() {
            return (
            <div>
            <div className="content">
            <p>information here</p>
            </div>
            </div>
            )
        }
    });

如何在另一个测试文件中要求comp1

我在文件末尾添加了module.exports,并且需要

var myComponents = require('../js/components/App.js').comp1

但是得到以下错误

  - TypeError: Cannot read property 'exports' of undefined

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

module.exports.comp1 = comp1;
module.exports.comp2 = comp2;

或altley:

module.exports = {comp1: comp1, comp2: comp2};

然后要求:

var Components = require('./path_to_file');
var comp1 = Components.comp1;
var comp2 = Components.comp2;