reactjs - React:向根 DOM 添加模态组件
问题描述
有没有办法决定组件的 DOM 父级?我需要能够添加一个组件,并且它必须驻留在 DOM 树中并且仍然是内部组件的子组件。
类似于以下内容:
<div className="row">
<Comp />
</div>
虽然 DOM 树是
<div id="root">
<div class="component">
...
用例是一个自定义模式,显示为一个覆盖。
解决方案
这是 React 门户的经典应用:它在选定的 DOM 节点(父节点)中渲染选定的组件
render() {
// React does *not* create a new div. It renders the children into `domNode`.
// `domNode` is any valid DOM node, regardless of its location in the DOM. ( body, a div etc etc )
return ReactDOM.createPortal(
this.props.children,
domNode
);
}
推荐阅读
- sql - 如何提高 Oracle SQL 中两个 SCD2 表的 JOIN 性能
- flutter - Flutter:在 Column 中完全包含 ListView(不展开)
- azure - 使用 AMQP 建立与事件中心的连接时如何设置 x-opt-offset 以避免消息重播
- r - 在数据框中添加新因子以替换另一个因子后,字母顺序无法正常工作
- powershell - ping 服务器时如何查看主机名?
- vue.js - 如何在 buefy 中添加 b 表列?
- java - spark-assembly-hadoop.jar:fat jar 剥离 BouncyCastle 提供者的签名
- jenkins - Jenkins 共享库管道:单例与按需单例
- ios - 在注册 Firebase 之前检查用户名
- azure-cosmosdb - Cosmos DB 上的 Gremlin 查询:无法将 StringField 类型的对象转换为 Compose1Field