【问题标题】:Loops inside react jsx render function with if else conditions内部循环使用 if else 条件反应 jsx 渲染函数
【发布时间】:2017-01-20 07:56:10
【问题描述】:

我有如下数据。

this.state.jsonData = [
        {
        category: "1",
        clientname: "rahul1",
        reporthtml: "hello1"
        },
        {
                category : "1",
                clientname: "rahul2",
                reporthtml: "hello2"
        },

        {
                category : "2",
                clientname: "rahul2",
                reporthtml: "hello2"
        },
         {
                category : "2",
                clientname: "rahul2",
                reporthtml: "hello2"
        },
        {
                category : "2",
                clientname: "rahul2",
                reporthtml: "hello2"
        },
];

现在我需要在 react jsx 中渲染它,如下所示。不能让 if else 只显示同一类别一次

我的html:

<div>
<div> Category Name</div>
<div>Client Name</div>
<div>Client Html</div>
<div>Client Name</div>
<div>Client Html</div>
<div>Client Name</div>
<div>Client Html</div>
</div>
<div>
<div> Category Name</div>
<div>Client Name</div>
<div>Client Html</div>
<div>Client Name</div>
<div>Client Html</div>
<div>Client Name</div>
<div>Client Html</div>
</div>


{this.state.jsonData.map(function(category,i) {
   return <div> category.category</div>// This line will only print once for each category need if else condition
   <div>Client Name</div>
   <div>Client Html</div>  ;
})}

【问题讨论】:

  • 使用Javascript map或reducer遍历对象数组

标签: javascript loops reactjs react-jsx


【解决方案1】:

如果我正确地推断出您想要什么,您只需使用嵌套的map:

render() {
    return <div>
        {this.state.jsonData.map(function(category) {
            return <div>
                <div>Category: {category.category}</div>
                {category.categorydata.map(function(data) {
                    return <div>
                        <div>Client: {data.clientname}</div>
                        <div>Report: {data.reporthtml}</div>
                    </div>;
                })}
                </div>;
        })}
    </div>;
}

现场示例:

class Category extends React.Component {
    constructor(...args) {
        super(...args);
        this.state = {};
        this.state.jsonData = [{
          category: "1",
          categorydata: [{
            clientname: "rahul1",
            reporthtml: "hello1"
          }, {
            clientname: "rahul2",
            reporthtml: "hello2"
          }, ]
        }, {
          category: "2",
          categorydata: [{
            clientname: "rahul1",
            reporthtml: "hello1"
          }, {
            clientname: "rahul2",
            reporthtml: "hello2"
          }, ]
        }];
    }

    render() {
        return <div>
            {this.state.jsonData.map(function(category) {
                return <div>
                    <div>Category: {category.category}</div>
                    {category.categorydata.map(function(data) {
                        return <div>
                            <div>Client: {data.clientname}</div>
                            <div>Report: {data.reporthtml}</div>
                        </div>;
                    })}
                    </div>;
            })}
        </div>;
    }
};

ReactDOM.render(
    <Category />,
    document.getElementById("react")
);
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

【讨论】:

  • 我改变了数组,在循环内我需要一些 if else 条件
【解决方案2】:

如果你有 es6,你可以像这样增强代码:

render() {
        return <div>
            {
                this.state.jsonData.map(category => <div>
                    <div>Category: {category.category}</div>
                    {
                        category.categorydata.map(data => <div>
                            <div>Client: {data.clientname}</div>
                            <div>Report: {data.reporthtml}</div>
                        </div>
                    )
                    }
                    </div>
            )}
        </div>
    }

【讨论】:

    猜你喜欢
    • 2019-09-11
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2020-06-20
    • 1970-01-01
    • 2020-10-12
    相关资源
    最近更新 更多