javascript - custom modal does not appear react.js
问题描述
I'm trying to call this Modal, but does not appear. I tried to put the Modal into a render() but it does not work too. I used inspect element tool and it shows the div from Modal but show it "grey".
This is the Modal
import React from 'react';
import './Modal.css';
const modal = props =>(
<div className="modal">
<header className="modal__header">
<h1>{props.title}</h1>
</header>
<section className="modal__content">
{props.children}
</section>
<section className="modal__actions">
{props.canCancel && (<button className="btn" onClick={props.onCancel}>Cancel</button>)}
{props.canConfirm &&(<button className="btn" onClick={props.onConfirm}>Confirm</button>)}
</section>
</div>
);
export default modal;
and I´m trying to call the Modal here
import React,{Component} from 'react';
import Modal from '../components/Modal/Modal.js';
import './Investments.css';
class InvestmentsPage extends Component{
render(){
return(
<React.Fragment>
<Modal>
<p>Modal content</p>
</Modal>
<div className="investment-control">
<p>Modal</p>
<button className="btn btnt1" >Crear </button>
</div>
</React.Fragment>);
}
}
export default InvestmentsPage;
解决方案
你的方法是错误的。我猜这里你的模态隐藏在其他一些 DOM 元素下。您应该将 ReactDOM.createPortal() 用于模式、工具提示等:
- 在id='root' 的现有 div 下的index.html中,使用 id='modal' 创建新的 div;
- 在上面列出的模态组件中,从 'react-dom' 导入 ReactDOM 并返回 ReactDOM.createPortal( your_JSX_here , document.querySelector('#modal'))
推荐阅读
- javascript - 构造“const increment = ()();”是什么?用 JavaScript 做什么?
- swift - 通过按下它来更改空数组中uiimageview的颜色
- r - 比较两列并在R中提取不匹配时如何描述
- math - 为什么我们乘以 2 PI 来创建一个圆
- java - 为什么 java 对象比较在 '==' 和 .equals() 方法上都失败了?
- javascript - 浏览器和nodejs上有没有可以将base64字符串转换为utf8字符串的API?
- kubernetes - 如何在 helmchart 中设置自定义发布名称
- php - Facebook登录并通过命令行获取帖子
- java - 如果在并发操作中不存在,则 JPA 插入实体
- node.js - MariaDB 连接到 ExpressJS 访问被拒绝,没有密码错误