来源:https://segmentfault.com/a/1190000011668286

Portals是react 16.3 提供的官方解决方案,使得组件可以脱离父组件层级挂载在DOM树的任何位置。

普通情况下,组件的render函数返回的元素会被挂载在它的父级组件上。

import DemoComponent from './DemoComponent';
render() {
// DemoComponent元素会被挂载在id为parent的div的元素上
return (
<div id="parent">
<DemoComponent />
</div>
);
}

然而,有些元素需要被挂载在更高层级的位置。最典型的应用场景:当父组件具有overflow: hidden或者z-index的样式设置时,组件有可能被其他元素遮挡,这个时候你就可以考虑要不要使用Portal使组件的挂载脱离父组件。例如:对话框,tooltip。

import DemoComponent from './DemoComponent';

render() {
// react会将DemoComponent组件直接挂载在真真实实的 dom 节点 domNode 上,生命周期还和16版本之前相同。
return ReactDOM.createPortal(
<DemoComponent />,
domNode,
);
}

组件的挂载点虽然可以脱离父组件,但组件的事件通过冒泡机制仍可以传给父组件。

官方domo

// These two containers are siblings in the DOM
const appRoot = document.getElementById('app-root');
const modalRoot = document.getElementById('modal-root'); class Modal extends React.Component {
constructor(props) {
super(props);
this.el = document.createElement('div');
} componentDidMount() {
modalRoot.appendChild(this.el);
} componentWillUnmount() {
modalRoot.removeChild(this.el);
} render() {
return ReactDOM.createPortal(
this.props.children,
this.el,
);
}
} class Parent extends React.Component {
constructor(props) {
super(props);
this.state = {clicks: };
this.handleClick = this.handleClick.bind(this);
} handleClick() {
// This will fire when the button in Child is clicked,
// updating Parent's state, even though button
// is not direct descendant in the DOM.
this.setState(prevState => ({
clicks: prevState.clicks +
}));
} render() {
return (
<div onClick={this.handleClick}>
<p>Number of clicks: {this.state.clicks}</p>
<p>
Open up the browser DevTools
to observe that the button
is not a child of the div
with the onClick handler.
</p>
<Modal>
<Child />
</Modal>
</div>
);
}
} function Child() {
// The click event on this button will bubble up to parent,
// because there is no 'onClick' attribute defined
return (
<div className="modal">
<button>Click</button>
</div>
);
} ReactDOM.render(<Parent />, appRoot);

 

react portals的更多相关文章

  1. react portals 插槽 实现简易弹窗

    Portal 提供了一种将子节点渲染到存在于父节点以外的DOM节点的优秀方案: 尽管 portal 可以被放置在 DOM 树中的任何地方,但在任何其他方面,其行为和普通的 React 子节点行为一致. ...

  2. ZooTeam 前端周刊|第 111期

    转: ZooTeam 前端周刊|第 111期 ZooTeam 前端周刊|第 111期 浏览更多往期周刊,请访问: https://weekly.zoo.team 基于Vue的前端架构,我做了这15点 ...

  3. 学习React系列(七)——Fragments、Portals、Error Boundaries与WEB组件

    React.Fragment portals Error Boundaries WEB组件 React.Fragment 想象一个场景,想把td包装为组件添加到table中去,代码如下: class ...

  4. react 插槽(Portals)

    前言: 昨天刚看了插槽,以为可以解决我工作中遇到的问题,非常激动,我今天又仔细想了想,发现并不能解决... 不过还是记录一下插槽吧,加深印象,嗯,就酱. 插槽作用: 插槽即:ReactDOM.crea ...

  5. [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 el ...

  6. React——组件的生命周期函数

    每一个组件都有一些生命周期函数. 当组件实例被创建并且会插入到DOM中,下面这些函数会被调用 constructor componentWillMount render componentDidMou ...

  7. [译文]React v16(新特性)

    [译文]React v16(新特性) 查看原文内容 我们很高兴的宣布React v16.0发布了! 这个版本有很多长期被使用者期待的功能,包括: fragments (返回片段类型) error bo ...

  8. React 特性剪辑(版本 16.0 ~ 16.9)

    Before you're going to hate it, then you're going to love it. Concurrent Render(贯穿 16) 在 18年的 JSConf ...

  9. 22.1 、react生命周期(一)

    在每个react组件中都有以下几个生命周期方法~我们需要在不同阶段进行讨论 组件生命周期概述 1.初始化 在组件初始化阶段会执行 constructor static getDerivedStateF ...

随机推荐

  1. vue,在模块中动态添加dom节点,并监听

    在这里,onclick事件没有作用,因为它指向的是window,如果写为this.click页面显示为undefined, 我采用的是通过class绑定事件,但是会有一个问题,那就是当你渲染多个事件时 ...

  2. ceph存储集群测试方案

    --测试目的 测试ceph集群的读写性能,根据测试数据了解整个ceph集群的性能情况. --测试环境 1.8节点ceph集群环境,1台虚拟机(cpu 8核,内存8G),8k的块大小,时长2小时 2.8 ...

  3. 周强201771010141《面向对象程序设计(java)》第一周学习总结

    周强201771010141<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com ...

  4. python基础--列表、元祖、字典、集合

    列表(List) 1.列表特点 列表是可变的!! list 是一种有序的序列,可以添加.删除其中的元素,并且可以通过下标(索引)访问 数据 2.简单的常用操作 A.通过下表访问元素 print(lis ...

  5. 大道至简第一章读后感JAVA伪代码形式

    观看了<大道至简>第一章后,从愚公移山的故事中可以发现愚公移山其实可以看作是一个一个项目,下面用JAVA伪代码形式编写: Import java(愚公移山) //愚公移山 public c ...

  6. vue-router(配置子路由--单页面多路由区域操作)

    1.配置子路由: import Post from "@components/Post" export default new Router({ routers:[ { path: ...

  7. prototype的一些事

    <script type="text/javascript"> foo=function(){ this.mayName="Foo function" ...

  8. 使用VS2015编译grpc_1.3.1

    环境: win7_x64,VS2015 开始: 一.安装工具 1. 安装cmake 2. 安装ActivePerl 3. 安装golang 4. 安装nasm 验证安装是否安装成功: cmake -v ...

  9. ipc基础

    ipc对象持久性 进程持久性:具有这种持久性的对象在持有它的最后一个进程关闭了该对象为止 内核持久性:这种IPC对象一直存在直到内核重新自举或显示删除该对象为止 文件系统持久性:具有这种持久性的对象只 ...

  10. GanttProject 项目管理软件的优点

    GanttProject 的优点 GanttProject 是一款基于 GPL 协议的开源软件,代码完全开源,使用的是 Java 语言编写. 最近在试用,有以下一些优大. 文件格式为 xml,这个是我 ...