Home > Web Front-end > JS Tutorial > What does portal mean in react?

What does portal mean in react?

coldplay.xixi
Release: 2020-11-17 14:02:26
Original
3566 people have browsed it

Portal in React provides a good way to render child nodes into DOM nodes in the DOM hierarchy outside the parent component. The syntax is [ReactDOM.createPortal(child, container)].

What does portal mean in react?

Portals Provides a nice way to render child nodes into DOM nodes in the DOM hierarchy outside the parent component.

Syntax:

ReactDOM.createPortal(child, container)
Copy after login

The first parameter (child) is any renderable React child element, such as an element, string or fragment. The second parameter (container) is a DOM element.

Usually an element returned from the component's render method. This element can only be assembled to its nearest parent element in the DOM node, but sometimes it is also necessary to insert it into different positions in the DOM node:

render() {
  // React 不会创建新的 div。他渲染子元素到 `domNode` 中。
  // `domNode` 可以是任意有效的 DOM 节点,无论他在 DOM 中的位置如何。
  return ReactDOM.createPortal(
    this.props.children,
    domNode,
  );
}
Copy after login

A typical use case for portal is when the parent component has overflow: hidden or z-index styles and you need its child components to visually jump out of it. container. For example, dialog boxes, hovercards, and tooltips.

Related free learning recommendations: JavaScript (video)

The above is the detailed content of What does portal mean in react?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template