Portals

Portals 提供了一种很好的将子节点渲染到父组件以外的 DOM 节点的方式。

const appRoot = document.getElementById('app-root');
//找一个modal入口挂载点
const modalRoot = document.getElementById('modal-root'); class Modal extends React.Component {
constructor(props) {
super(props);
//创建组件容器
this.el = document.createElement('div');
} componentDidMount() {
//dom渲染好,将modal容器加入到modalRoot里面
modalRoot.appendChild(this.el);
} componentWillUnmount() {
//组件销毁到时候删除掉modal容器
modalRoot.removeChild(this.el);
} render() {
//使用createPortal指定挂载位置
return ReactDOM.createPortal(
//接收组件
this.props.children,
//将组件加入到modal容器里
this.el,
);
}
} class App extends React.Component {
constructor(props) {
super(props);
this.state = {showModal: false}; this.handleShow = this.handleShow.bind(this);
this.handleHide = this.handleHide.bind(this);
} handleShow() {
this.setState({showModal: true});
} handleHide() {
this.setState({showModal: false});
} render() {
const modal = this.state.showModal ? (
<Modal>
//传入组件内容
<div className="modal">
<div>
With a portal, we can render content into a different
part of the DOM, as if it were any other React child.
</div>
This is being rendered inside the #modal-container div.
<button onClick={this.handleHide}>Hide modal</button>
</div>
</Modal>
) : null; return (
<div className="app">
This div has overflow: hidden.
<button onClick={this.handleShow}>Show modal</button>
{modal}
</div>
);
}
} ReactDOM.render(<App />, appRoot);

Portals的更多相关文章

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

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

  2. Angular动态创建组件之Portals

    这篇文章主要介绍使用Angular api 和 CDK Portals两种方式实现动态创建组件,另外还会讲一些跟它相关的知识点,如:Angular多级依赖注入.ViewContainerRef,Por ...

  3. react 插槽(Portals)

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

  4. Codeforces 196E Opening Portals MST (看题解)

    Opening Portals 我们先考虑如果所有点都是特殊点, 那么就是对整个图求个MST. 想在如果不是所有点是特殊点的话, 我们能不能也 转换成求MST的问题呢? 相当于我们把特殊点扣出来, 然 ...

  5. react portals

    来源:https://segmentfault.com/a/1190000011668286 Portals是react 16.3 提供的官方解决方案,使得组件可以脱离父组件层级挂载在DOM树的任何位 ...

  6. C - Portals Gym - 102006C (网络流最小割)

    题目链接:https://cn.vjudge.net/contest/283918#problem/C 题目大意:T个测试数据,然后给你一个字符串,每一个字符串包括‘s’,‘t’,‘o’,‘#’,‘. ...

  7. [CodeForces - 197F] F - Opening Portals

    F - Opening Portals Pavel plays a famous computer game. A player is responsible for a whole country ...

  8. xtu summer individual 3 F - Opening Portals

    Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  9. [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 ...

随机推荐

  1. ORA-03113 : end-of-file on communication channel

    现象一: 数据库startup时,出现数据库无法正常mount,并报ORA-03113错误. SQL> startup ORACLE instance started. Total System ...

  2. 八大排序算法的python实现(二)希尔排序

    代码: #coding:utf-8 #author:徐卜灵 # 希尔排序的实质就是分组插入排序,该方法又称缩小增量排序,因DL.Shell于1959年提出而得名. # 希尔排序,也称递减增量排序算法, ...

  3. 重写成员“MySql.Data.Entity.MySqlConnectionFactory.CreateConnection(System.String)”时违反了继承安全性规则。重写方法的安全可访问性必须与所重写方法的安全可访问性匹配。

      1,程序中使用加载反射出现下面的问题: 无法加载一个或多个请求的类型.有关更多信息,请检索 LoaderExceptions 属性. 然后把代码改了一下, try { types.AddRange ...

  4. Eclipse 使用TFS

    Install Soft , –> add http://dl.microsoft.com/eclipse/tfs   form:http://msdn.microsoft.com/en-us/ ...

  5. Spring学习笔记(六)—— SSH整合

    一.整合原理 二.整合步骤 2.1 导包 [hibernate] hibernate/lib/required hibernate/lib/jpa 数据库驱动 [struts2] struts-bla ...

  6. day--42 前端基础小结

    前端基础总结 一:前端实现的原理: 小实例: 01:第一步:创建一个socket服务端: import socket server=socket.socket() ip_port=("127 ...

  7. php 对象方式传入参数

    类是单例模式,对象方式传入参数,如果参数过多是,使用形参容易混乱 class Object { /** * 基本配置信息 * @var array */ private $config = array ...

  8. POJ_1019 Number Sequence 【递推】

    题目: A single positive integer i is given. Write a program to find the digit located in the position ...

  9. 113th LeetCode Weekly Contest Largest Time for Given Digits

    Given an array of 4 digits, return the largest 24 hour time that can be made. The smallest 24 hour t ...

  10. HOW TO MAKE IT FLOW ?

      FLEXnet Licensing is a member of the FLEXnet Publisher family of products. It is the most popular ...