[英]Side effect in MUI Modal
I use React 17.0.1 and Material-UI 4.11.2 in my project.我在我的项目中使用 React 17.0.1 和 Material-UI 4.11.2。 When I try to make a side effect for modal in the useEffect hook it executes before a modal rendering.当我尝试在 useEffect 钩子中为模态生成副作用时,它会在模态渲染之前执行。
import React, { useEffect } from "react";
import { Modal } from '@material-ui/core';
const MyModal = ({ sideFunction, isModalVisible, closeModal }) => {
useEffect(() => {
sideFunction();
}, []);
return (
<Modal open={isModalVisible} onClose={closeModal}>
<div>MODAL</div>
</Modal>
);
}
Try to render your model conditionaly where you have used this model.尝试在您使用此 model 的地方有条件地渲染您的 model。 instead of:代替:
<MyModal ...props />
Use Like this:像这样使用:
{isModalVisible && <MyModal ...props />}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.