英文:
How to close MUI Modal opened from parent component inside child component react
问题
我需要创建一个模态组件,它可以从父组件中打开并从模态组件本身关闭,使用React。
我在父组件中设置了状态,并将其作为属性传递给子模态组件:
const [modalIsOpen, setModalIsOpen] = useState(false);
const handleOpenFromParent = () => {
setModalIsOpen(true);
}
const handleCloseFromChild = () => {
setModalIsOpen(false);
}
和
<div
className={`${styles.w_30} p-3 d-flex justify-content-center align-items-center flex-column`}
onClick={handleOpenFromParent}
>
<ModalPaymentMethods
playerNickname={nickname}
paymentDescription={paymentMethodsVisa}
paymentLogo={<Icon icon='mdi:credit-card-outline' color={'#84c3d3'} style={{ fontSize: '150px' }} />}
depositTimes={11}
depositAmount={'€18.6k'}
depositTimesLastSixMonths={9}
depositQuantityLastSixMonths={'€12.7k'}
paymentMethodNumber={4301165517084005}
expiryMonth={'07'}
expiryYear={'2017'}
holderName={`${name + ' ' + lastName}`}
billingAddress={address}
modalIsOpen={modalIsOpen}
handleCloseFromChild={handleCloseFromChild}
/>
</div>
我能够从父组件中打开模态,但是无法从子组件中关闭它。
我将回调handleCloseFromChild
作为属性传递,以便在模态上将可见状态设回false
,但它不起作用,modalIsOpen
始终保持为 true,从回调函数中永远不会设置为 false:
const ModalPaymentMethods = ({
playerNickname,
paymentDescription,
paymentLogo,
depositTimes,
depositAmount,
depositTimesLastSixMonths,
depositQuantityLastSixMonths,
paymentMethodNumber,
expiryMonth,
expiryYear,
holderName,
billingAddress,
modalIsOpen,
handleCloseFromChild
}) => {
console.log(modalIsOpen);
console.log(handleCloseFromChild);
return (
<div className={styles.ModalPaymentMethods}>
<Modal
open={modalIsOpen}
onClose={handleCloseFromChild}
aria-labelledby='modal-modal-title'
aria-describedby='modal-modal-description'
>
test
</Modal>
</div>
);
}
英文:
I need to build a modal component that is opened from a parent component and closed from the modal component itself in React.
I'm setting the state in the parent component and passed it down to the child modal component as props:
const [modalIsOpen, setModalIsOpen] = useState(false)
const handleOpenFromParent = () => {
setModalIsOpen(true)
}
const handleCloseFromChild = () => {
setModalIsOpen(false)
}
and
<div
className={`${styles.w_30} p-3 d-flex justify-content-center align-items-center flex-column`}
onClick={handleOpenFromParent}
>
<ModalPaymentMethods
playerNickname={nickname}
paymentDescription={paymentMethodsVisa}
paymentLogo={<Icon icon='mdi:credit-card-outline' color={'#84c3d3'} style={{ fontSize: '150px' }} />}
depositTimes={11}
depositAmount={'€18.6k'}
depositTimesLastSixMonths={9}
depositQuantityLastSixMonths={'€12.7k'}
paymentMethodNumber={4301165517084005}
expiryMonth={'07'}
expiryYear={'2017'}
holderName={`${name + ' ' + lastName}`}
billingAddress={address}
modalIsOpen={modalIsOpen}
handleCloseFromChild={handleCloseFromChild}
/>
</div>
I have no problems in opening the modal from the parent, but I'm not managing to close it once opened, from the child component.
I'm passing a callback handleCloseFromChild
as a prop to set the visibility state back to false on the modal but it's not working, modalIsOpen
remains always true and is never set to false from the callback function:
const ModalPaymentMethods = ({
playerNickname,
paymentDescription,
paymentLogo,
depositTimes,
depositAmount,
depositTimesLastSixMonths,
depositQuantityLastSixMonths,
paymentMethodNumber,
expiryMonth,
expiryYear,
holderName,
billingAddress,
modalIsOpen,
handleCloseFromChild
}) => {
console.log(modalIsOpen)
console.log(handleCloseFromChild)
return (
<div className={styles.ModalPaymentMethods}>
<Modal
open={modalIsOpen}
onClose={handleCloseFromChild}
aria-labelledby='modal-modal-title'
aria-describedby='modal-modal-description'
>
test
</Modal>
</div>
)
答案1
得分: 4
原因是ModalPaymentMethods
组件位于具有onClick
事件的div
元素内部。
只需尝试在handleOpenFromParent
和handleCloseFromChild
函数中都添加控制台日志,然后您可以找到问题。
您可以简单地通过使用stopPropagation
方法解决这个问题。
const handleCloseFromChild = (e) => {
e.stopPropagation();
setModalIsOpen(false);
}
英文:
The reason is that ModalPaymentMethods
component inside the div element which has onClick
event.
Just try write console log on both handleOpenFromParent
and handleCloseFromChild
function, then you can find the problem.
You can simply solve that problem by using stopPropagation
method.
const handleCloseFromChild = (e) => {
e.stopPropagation();
setModalIsOpen(false)
}
答案2
得分: 0
你应该像这样定义一个开启状态:
const [open, setOpen] = useState(false)
之后,定义一个处理它的函数:
const handleToggleModal = () => { setOpen((current) => !current) }
你应该将这个 handleToggleModal
函数作为 props 传递给子组件,并在需要关闭模态框的地方调用它。
open
状态被用作一个开关布尔值,用于关闭和打开模态框。
英文:
You should define a open state like this:
const [open,setOpen]=useState(false)
After that define a function to handle it:
const handleToggleModal=()=>{setOpen((current)=>!current)}
and you should pass this handleToggleModal
function as props to the child component and call it everywhere you need to close the modal.
open
state is used as a toggle boolean value to close and open the modal
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论