javascript - react render里面多次循环,怎么破
阿神
阿神 2017-04-11 09:17:33
[JavaScript讨论组]
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 (
                <p>
                    <Head/>
                    <p 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>
                    </p>
                </p>
            );
        }
    }
阿神
阿神

闭关修行中......

全部回复(4)
巴扎黑

感觉要么就是你把处理数据的这部分代码放到别的地方去做,要么就是重构逻辑,减少循环嵌套。

PHP中文网

用 react 话,不是可以更好的组建化?
你可以将每个循环中的结构单独提为一个组件:Item1,Item2,Item3
当前组件中 map <Item1>
<Item1> 中 map <Item2>
<Item2> 中 map <Item3>。

高洛峰

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(
                        <p key={''+index1+index2+index3}>
                            <p>{item3.describle}</p>
                            <pre><code className={item3.codeType}>{item3.code}</code></pre>
                        </p>
                    );
                });
            });
        });
        return (
            <p>
                <Head/>
                <p 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>
                </p>
            </p>
        );
    }
}
PHP中文网

冲这个嵌套的程度,我觉得你应该改变结构了,你现在走的路会让你更痛苦。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号