zoukankan      html  css  js  c++  java
  • [React] Render Elements Outside the Current React Tree using Portals in React 16

    By default the React Component Tree directly maps to the DOM Tree. In some cases when you have UI elements like overlays or loading bars this can be limiting. React 16 therefor ships with the new portal feature allowing you to attach parts of the Component tree inside a different root element.

    This lesson explores the new functionality by building an <Overlay/> component that renders out its children and creates a closeable overlay outside its DOM node.

    It is important to understand what portals is good for. 

    Let's see what if we don't have portals:

    As we can see, the overlay component is nested inside our wrapper component:

    Now let's see what if we use portals:

    This time we have created a empty 'overlay-root' div inside the html body.

      <body>
        <div id="root"></div>
        <div id="overlay-root">
          
        </div>
      </body>

    And inside our Overlay compmonet, we render the component into this 'overlay-root' div:

     If now we open devtool to see DOM structure:

    As we can see Overlay-root is spreated from wrapper component, and it is reuseable and provide a much clean DOM structure.

  • 相关阅读:
    jsTree展开根节点 设置用户图标
    Js图片缩放代码 鼠标滚轮放大缩小 图片向右旋转
    MySQL删除重复数据
    200道物理学难题——001 三只蜗牛
    慎用GetOpenFileName
    Windows7隐藏字体
    Windows 位图
    Windows 调色板
    C++模板特化
    使用Visual Studio制作安装包
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7608520.html
Copyright © 2011-2022 走看看