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.

[React] Render Elements Outside the Current React Tree using Portals in React 16的更多相关文章

  1. react hooks & props change & pagination current bug

    react hooks & props change & pagination current bug multi tables & pigination bug & ...

  2. React.render和reactDom.render的区别

    刚开始学习react.js.发现网上的资料,有些是写着react.render,有些写着reactDom.render.觉得很奇怪就查阅了一下资料.解释如下: 这个是react最新版api,也就是0. ...

  3. ReactDom.render和React.render的区别

    这个是react最新版api,也就是0.14版本做出的改变.主要是为了使React能在更多的不同环境下更快.更容易构建.于是把react分成了react和react-dom两个部分.这样就为web版的 ...

  4. react render

    实际上react render方法返回一个虚拟dom 并没有去执行渲染dom 渲染的过程是交给react 去完成的 这就说明了为什么要在所有数据请求完成后才去实现render 这样做也提高了性能.只调 ...

  5. React Render Callback Pattern(渲染回调模式)

    React Render Callback Pattern,渲染回调模式,其实是将this.props.children当做函数来调用. 例如: 要根据user参数确定渲染Loading还是Profi ...

  6. React render algorithm & Fiber vs Stack

    React render algorithm & Fiber vs Stack React 渲染算法 & Fiber vs Stack https://stackoverflow.co ...

  7. React Creating Elements All In One

    React Creating Elements All In One https://reactjs.org/docs/react-api.html#creating-react-elements h ...

  8. React Transforming Elements All In One

    React Transforming Elements All In One https://reactjs.org/docs/react-api.html#transforming-elements ...

  9. React render twice bug

    React render twice bug React bug constructor render twice bug update render twice bug StrictMode htt ...

随机推荐

  1. 29.AngularJS 简介

    转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 H ...

  2. HIVE JOIN_1

    HIVE JOIN 概述 Hive join的实现包含了: Common (Reduce-side) Join Broadcast (Map-side) Join Bucket Map Join So ...

  3. 配置CiscoWorks 2000 ANI同步

    配置CiscoWorks 2000 ANI同步       在CiscoWorks 2000的LAN ManagementSolution(LMS)中,Cisco包含了一种ANI的自动发现过程和Res ...

  4. UltraEdit Companion Utility

    UltraEdit Companion Utility 配色组件 http://www.danielwmoore.com/extras/index.php?action=downloads;sa=vi ...

  5. 分享一个正则 选择html中所有的单标签

    var str = /\B<.+?>/g;

  6. JQ遍历 input 并修改name属性

    1.执行完克隆行后,会出现name属相相同的问题 function addRow(){ var obj = $("tr[name='info']:last"); var objCl ...

  7. [Python] isinstance() for checking object type

    isinstance("foo", str) isinstance(1, int) isinstance(4.0, float)

  8. 知名游戏开发者称 C++ 是一种非常糟糕、可怕的语言(C++不是一门可怕的语言,可怕的是一群没有耐心的程序员来使用C++这门语言)

    抛出一个问题:C++ 真的很可怕吗? 2016 年底,C++ 之父 Bjarne Stroustrup 在一次采访中表示:”C++ 让编程专家很容易编写出复杂.高性能.低资源消耗的代码,但不足以成为广 ...

  9. Resize图片

    在网站上传图片的时候,提示图片太大了. 有5种方式来调整图片大小 http://www.wikihow.com/Resize-a-JPEG picresize.com 这个网站比较靠谱:使用Windo ...

  10. Android圆形图片不求人,自定义View实现(BitmapShader使用)

    在很多APP当中,圆形的图片是必不可少的元素,美观大方.本文将带领读者去实现一个圆形图片自定View,力求只用一个Java类来完成这件事情. 一.先上效果图 二.实现思路 在定义View 的onMea ...