简体   繁体   English

React:如何通过单击隐藏组件?

[英]React: How to hide a component by clicking on it?

I am trying to make a React component hidden by clicking on it but all I see online are explanations about event handlers on buttons and the like.我试图通过单击它来隐藏一个 React 组件,但我在网上看到的只是关于按钮等事件处理程序的解释。

I'm using Next.js (but I don't think these means much for this).我正在使用 Next.js (但我认为这些对此意义不大)。

This is my component:这是我的组件:

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>
    )
}

Try setting a state on click of your component.尝试在单击组件时设置 state。 Below code should work.下面的代码应该可以工作。

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>
    )
}

Hope it helps.希望能帮助到你。

You could use a state property which tells you whether you should hide the component or not.您可以使用 state 属性来告诉您是否应该隐藏组件。 Based on that state, conditionally render another css class to the component you want to hide, using the classnames package (you will need to preinstall it npm install --save classnames ) Based on that state, conditionally render another css class to the component you want to hide, using the classnames package (you will need to preinstall it npm install --save classnames )

import React, {useState} from 'react';
import classes from './Component.module.css';
import classNames from 'classnames';

const Component = props => {

    const [show, setShow] = useState(true);

    return (
        <div className={classes.Component} onClick={() => setShow(!show)}>
            <div
                className={classNames( {
                    [classes.Show]: true,
                    [classes.Disappear]: !show
                })}
            >
                {props.children}
            </div>
        </div>
    );
};

export default Component;

In the Disappear css class, you can use whatever css properties you need to make your component disappear in a more elegant way, such as display: none;在 Disappear css class 中,您可以使用任何您需要的 css 属性以更优雅的方式使您的组件消失,例如display: none; or visibility: hidden;visibility: hidden; (including transitions) (包括过渡)

Of course, if what you are looking for is just not rendering the component at all, the standard "wrapping the div in an if statement" shown in the other answers is a perfectly valid solution.当然,如果您正在寻找的只是根本不渲染组件,那么其他答案中显示的标准“将 div 包装在 if 语句中”是一个完全有效的解决方案。

You need to create a state variable which will determine to show popup or not.您需要创建一个 state 变量,该变量将确定是否显示弹出窗口。 You can achieve it by using useState hook.您可以通过使用 useState 钩子来实现它。

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


export default function Popup() {
 const [isPopupVisible,setPopupVisibility] = useState(true);

    return (
        <div className={styles.popup}>
           { isPopupVisible && (<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 className={styles.buttonContainer}><button className={styles.button} onClick={setPopupVisibility(false)}>Okay</button></div>

            </div>)}
        </div>
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM