gpt4 book ai didi

javascript - react : How to hide a component by clicking on it?

转载 作者:行者123 更新时间:2023-12-03 13:34:00 26 4
gpt4 key购买 nike

我试图通过单击 React 组件来隐藏它,但我在网上看到的只是有关按钮等事件处理程序的解释。

我正在使用 Next.js(但我认为这些对此没有多大意义)。

这是我的组件:

import styles from './styles/popup.module.scss';
import React, { Component } from "react";


export default function Popup() {
return (
<div className={styles.popup}>
<div className={styles.popupInner}>
<h1>Temporary Closure</h1>
<p>
Following the Australian government’s directive to keep our nation safe and limit the spread of Coronavirus (Covid-19), it is with great sadness that we advise that SS will be temporarily closed, effective 23<sup>rd</sup> March 2020
</p>
</div>
</div>
)
}

最佳答案

尝试在单击组件时设置状态。下面的代码应该可以工作。

import styles from './styles/popup.module.scss';
import React, { Component } from "react";


export default function Popup() {

const [visible, setVisible] = React.useState(true);

if(!visible) return null;

return (
<div className={styles.popup} onClick={() => setVisible(false)}>
<div className={styles.popupInner}>
<h1>Temporary Closure</h1>
<p>
Following the Australian government’s directive to keep our nation safe and limit the spread of Coronavirus (Covid-19), it is with great sadness that we advise that sydney sauna will be temporarily closed, effective 23<sup>rd</sup> March 2020
</p>
<div className={styles.buttonContainer}><button className={styles.button}>Okay</button></div>

</div>
</div>
)
}

希望有帮助。

关于javascript - react : How to hide a component by clicking on it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61769428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com