开发者社区> 问答> 正文

react render里面多次循环,怎么破

render(){
        if(this.state.loading){
            return <Loading/>;
        }else{
            let [header={},bodyHtml='']=[this.state.articleDetail.header];
            bodyHtml=this.state.articleDetail.body.content.map(function (item1,index1) {
                bodyHtml+=<h4>{item1.firstTitle}</h4>;
                item1.firstTitleDes.map(function (item2,index2) {
                    bodyHtml+=<h6>{item2.secondTitle}</h6>;
                    item2.secondTitleDes.map(function (item3,index3) {
                        bodyHtml+=<p>{item3.describle}</p>;
                        bodyHtml+=<pre><code className={item3.codeType}>{item3.code}</code></pre>;
                    });
                });
                return (bodyHtml);
            });
            return (
                <div>
                    <Head/>
                    <div className="container">
                        <header className="aticle-header">
                            <h2>{header.title}</h2>
                            <p>发表于&nbsp;&nbsp;{header.date}&nbsp;&nbsp;|&nbsp;&nbsp;in&nbsp;&nbsp; <Link to="/">{header.type}</Link></p>
                        </header>
                        <article className="aticle-body">{bodyHtml}</article>
                    </div>
                </div>
            );
        }
    }

展开
收起
a123456678 2016-03-11 10:13:05 4681 0
1 条回答
写回答
取消 提交回答
  • render(){
    
    全选复制放进笔记    if(this.state.loading){
            return <Loading/>;
        }else{
            let [header={},bodyHtml=[]]=[this.state.articleDetail.header];
            this.state.articleDetail.body.content.forEach(function (item1,index1) {
                bodyHtml.push(<h4 key={index1}>{item1.firstTitle}</h4>);
                item1.firstTitleDes.forEach(function (item2,index2) {
                    bodyHtml.push(<h6 key={''+index1+index2}>{item2.secondTitle}</h6>);
                    item2.secondTitleDes.forEach(function (item3,index3) {
                        bodyHtml.push(
                            <div key={''+index1+index2+index3}>
                                <p>{item3.describle}</p>
                                <pre><code className={item3.codeType}>{item3.code}</code></pre>
                            </div>
                        );
                    });
                });
            });
            return (
                <div>
                    <Head/>
                    <div className="container">
                        <header className="aticle-header">
                            <h2>{header.title}</h2>
                            <p>发表于&nbsp;&nbsp;{header.date}&nbsp;&nbsp;|&nbsp;&nbsp;in&nbsp;&nbsp; <Link to="/">{header.type}</Link></p>
                        </header>
                        <article className="aticle-body">{bodyHtml}</article>
                    </div>
                </div>
            );
        }
    }
    2019-07-17 18:58:32
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
利用编译将 Vue 组件转成 React 组件 立即下载
React Native 全量化实践 立即下载
React Native项目实战优化之路 立即下载