需要时导入反应组件

时间:2017-11-20 16:52:54

标签: javascript reactjs import

我需要你的帮助。我想用反应渲染三个标签。问题是:我需要按照他们的订单(1,2,0)对标签进行排序。我在 app.js 中创建了函数 getTabsTitlesInOrder(),我想下载所需的文件并将其分配给适当的内容[" tab&#34 ]。在这种情况下有没有办法下载文件?

已更新

新问题:现在我遇到错误 未捕获错误:对象作为React子对象无效(找到:带键的对象{default})。如果你想渲染一个孩子的集合,请使用数组。 帮我修复它!

我的档案: 的 tabs.json

{
   "tabs": [
      {
         "title": "tabA",
         "order": 1,
         "path": "./tabA.js",
      },
      {
         "title": "tabB",
         "order": 2,
         "path": "./tabB.js",
      },
      {
         "title": "tabC",
         "order": 0,
         "path": "./tabC.js",
      },
   ]
}

tabA.js tabB.js tabC.js 相同):

import React from 'react';

const TabA = createReactClass({
   render: function(){
      return(
         <div className="tabAContent">
            <p>Content of Tab A</p>
         </div>
      );
   }
)};

export default TabA;

app.js

const tabsJson = require('../tabs.json');
import React from 'react';
import ReactDOM from 'react-dom';

class Tabs extends React.Component{
    render(){
        return(
            <div className="tabs">
                {React.Children.map(this.props.children, (child, i) => {
                    return(
                        <div 
                            className={"tabTitle " + (child.key === this.props.active ? "tabTitleActive" 
                                        : " ")}
                            onClick={() => {
                                this.props.onChange(child.key);
                            }}>
                            {child}
                        </div>
                    );
                })}
            </div>
        );
    }
};


class App extends React.Component{
    constructor(props) {
        super(props);
        this.state = {active: 'tab1'};
    }

    render() { 
        var firstTabTitle, secondTabTitle, thirdTabTitle;
        var content = {};

        var requireTab = require.context("./tabs", true, /^.*\.js$/);

        const getTabsTitlesInOrder = () => {
            for(var i = 0; i < tabsJson.tabs.length; i ++){
                switch (tabsJson.tabs[i].order){
                    case 0:
                        firstTabTitle = tabsJson.tabs[i].title;

                        content["tab1"] = 
                                requireTab(tabsJson.tabs[i].path);

                        break;
                    case 1:
                        secondTabTitle = tabsJson.tabs[i].title;

                        content["tab2"] = 
                                requireTab(tabsJson.tabs[i].path);

                        break;
                    case 2:
                        thirdTabTitle = tabsJson.tabs[i].title;

                        content["tab3"] = 
                                requireTab(tabsJson.tabs[i].path);

                        break;
                }
            }
            getTabsContentInOrder (firstTabTitle, secondTabTitle, thirdTabTitle);
        };

        return(
            <div>
                <div className="tabButtons">
                    <Tabs 
                        active = {this.state.active}
                        onChange = {active => this.setState({active})}
                    >
                        <div key="tab1">{firstTabTitle}</div>
                        <div key="tab2">{secondTabTitle}</div>
                        <div key="tab3">{thirdTabTitle}</div>
                    </Tabs>
                </div>
                <div className="tabContent">{content[this.state.active]}</div>
            </div>
        );
    }
};

ReactDOM.render(
    <App />,
    document.getElementById('tab')
);

1 个答案:

答案 0 :(得分:0)

https://webpack.js.org/guides/dependency-management/#require-context

var requireTab = require.context("./tabs", true, /^tab[A-Z]\.js$/);
...
if (tab === 'A') {
   const content = requireTab('tabA')
...