How to show and hide divs in react

藏色散人
Release: 2023-01-18 13:58:15
Original
2705 people have browsed it

React method to display and hide divs: 1. Use "{showoverlay? (

):null}" in the functional component content to display and hide divs; 2. Determine the value of visible in the business logic and set "style={{ display: `${visible ? '' : 'none'}` }}" in the component style.

How to show and hide divs in react

The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.

How to show and hide divs in react?

How to hide and show a component under react functional component (two methods)

The first method

1. Function The formula component

code is as follows (example):

//函数式组件内容中
 const [showoverlay, setshowoverlay] = useState(false);
//渲染时,运算符
return(
     <>
     {showoverlay? (<div>显示或隐藏</div>):null}
     </>
)
Copy after login

2. Class component

The online examples are basically operations under class components.

The code is as follows (example):

//构造函数中
constructor(props) {
    super(props);
    this.state = {showWarning: true}
  }
 //渲染时
      <>
  { this.state.showWarning?
        <div>显示或隐藏</div> :null
        }
      </>
Copy after login

Second method

//在业务逻辑中判断visible的取值
const [visible, setVisible] = useState<boolean>(false);
//组件样式中设置
<div className="overlaydiv" ref={overlayContainerRef} style={{ display: `${visible ? &#39;&#39; : &#39;none&#39;}` }}>
   组件内容
</div>
Copy after login

Recommended learning: "react video tutorial"

The above is the detailed content of How to show and hide divs in react. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!