javascript - React Modal 未通过 OnClick 关闭
问题描述
我已经设置了一个 onClick 方法来关闭 React 模式,但它没有改变状态。我相信可能存在问题,openModalTwo, setOpenModalTwo
但我不确定。
这是我的代码:
import { Avatar } from "@material-ui/core";
import React, { useState } from "react";
import { useSelector } from "react-redux";
import { selectUser } from "../features/userSlice";
import "../style/QuestionBox.css";
import Modal from "react-modal";
import PeopleAltOutlinedIcon from "@material-ui/icons/PeopleAltOutlined";
import { ExpandMore } from "@material-ui/icons";
import { Input } from "@material-ui/core";
import LinkIcon from "@material-ui/icons/Link";
import db from "../firebase";
import firebase from "firebase";
function QuestionBox() {
const user = useSelector(selectUser);
const [openModalTwo, setOpenModalTwo] = useState(false);
const [input, setInput] = useState("");
const [inputUrl, setInputUrl] = useState("");
const handleQuestion = (e) => {
e.preventDefault();
db.collection("questions").add({
question: input,
imageUrl: inputUrl,
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
user: user,
});
setInput("");
setInputUrl("");
setOpenModalTwo(false);
};
return (
<div className="questionBox" onClick={() => setOpenModalTwo(true)}>
<div className="questionBox__info">
<Avatar src={user.photo} />
<h5>{user.displayName}</h5>
</div>
<div className="questionBox__question">
<p>Where do I start?</p>
</div>
<Modal
isOpen={openModalTwo}
onRequestClose={() => setOpenModalTwo(false)}
shouldCloseOnOverlayClick={false}
style={{
overlay: {
width: 700,
height: 600,
backgroundColor: "transparent",
boxShadow:
"box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);",
zIndex: "1000",
top: "50%",
left: "50%",
marginTop: "-300px",
marginLeft: "-350px",
},
}}
>
<div className="modal__title">
<h5>Add Question Here</h5>
<h5>Share Your Question</h5>
</div>
<div className="modal__info">
<Avatar Avatar className="avatar" src={user.photo} />
<p>
{user.displayName ? user.displayName : user.email} wants to know
</p>
<div className="modal__scope">
<PeopleAltOutlinedIcon />
<p>Public</p>
<ExpandMore />
</div>
</div>
<div className="modal__field">
<Input
required
value={input}
onChange={(e) => setInput(e.target.value)}
type="text"
placeholder="Ask where to start on your project with a specific 'How' or 'What' question."
/>
<div className="modal__fieldLink">
<LinkIcon />
<input
value={inputUrl}
onChange={(e) => setInputUrl(e.target.value)}
type="text"
placeholder="Add a link to help others understand what you want to build."
/>
</div>
<div className="modal__buttons">
<button
onClick={() => setOpenModalTwo({ openModalTwo: false })}
className="cancel"
>
Close
</button>
<button onClick={handleQuestion} type="submit" className="add">
Add Question
</button>
</div>
</div>
</Modal>
</div>
);
}
export default QuestionBox;
解决方案
不知道究竟是如何react-modal
工作的,但是当您设置setOpenModalTwo(true)
单击模态的父容器时,当您尝试关闭模态(我猜是通过单击)时,您也会触发onClick
父 div 的事件,所以重新打开模态。
所以要么将你的模态移到你的 div 之外,或者event.preventDefault()
在模态请求关闭时使用
推荐阅读
- batch-file - 如果批处理脚本中的语句不起作用?
- android - 如何使用等效于特定 dp 的 rQuadTo 创建带圆角的路径?
- java - 在双引号外的逗号上拆分。如果是单引号,则忽略双引号
- python - 在 python 中使用 ftplib 上传文件
- c++ - 如何从其内部指针访问 unique_ptr?
- database - 如何以管理员身份访问云上的 IBM DB2 仓库
- java - 如何使用 https://github.com/alamkanak/Android-Week-View 设置下个月的事件?
- c# - 只有 C# 匿名类型中允许的属性
- swift - 无法识别的选择器发送到实例,通知中心获取键盘大小
- android - 我的应用程序在第一次登录后显示空白屏幕 2 分钟