在React.cloneElement之下再使用this.props.children会显示两遍组件

代码传到github了点这里

代码传到github了点这里

代码传到github了点这里

  1. npm i

  2. npm start

  3. 浏览器输入地址 http://localhost:3011/

下面大致描述一下问题:
代码结构是这样的

  • App组件 (使用{ React.cloneElement(this.props.children, this.props) } )

    • itemList组件 (使用this.props.children)

      • oneItem组件 (一些内容)

//App.js
//使用React.cloneElement是为了把一些redux得到的porps传过去问题本身和redux本身无关
  <div className="content-wrap">
        <content className="content-main">
          { React.cloneElement(this.props.children, this.props) }
        </content>
   </div>

//itemList.js
  <div>
        <ul>
            <li><Link to="/items/1">1</Link></li>
            <li><Link to="/items/2">2</Link></li>
            <li><Link to="/items/3">3</Link></li>
            <li><Link to="/items/4">4</Link></li>
        </ul>
        {this.props.children}
  </div>

//oneItme.js
<h2>
    这是{this.props.parmas.id}的详情
</h2>

App.js使用{ React.cloneElement(this.props.children, this.props) }
图片描述

App.js使用{this.props.children}
图片描述

怎么解决这种显示两次的问题?

阅读 3.5k
1 个回答

{ React.cloneElement(this.props.children, this.props) }

你把 this.props 里的 children 传给 this.props.children 啦!

不懂你为什么要 clone 一下。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题