Hide And Show Element Using ReactJS


In this ReactJS article, we learn how to hide and show a DOM element. When we click on a button, the element will hide from the DOM and after clicking it again it will show again, so it will give a toggle effect.
What is ReactJS?
React is an open-source JavaScript library for building user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. React can be used as a base in the development of single-page or mobile applications.
  • Basic knowledge of React
  • Visual Studio Code must be installed
  • Node JS must be installed

Create ReactJS project

Step 1
The very first step is to create a new React.js project using the following command:
  1. npx create-react-app reactHideShow
Step 2
Open the newly-created project in Visual Studio code and install React bootstrap in this project using the following command:
  1. npm install bootstrap   
Step 3
Now, open the index.js file and import Bootstrap.
  1. import 'bootstrap/dist/css/bootstrap.min.css';   
Step 4
The next step is to open App.js and paste the below code:
  1. import React from 'react';  
  2. import './App.css';  
  3. import { Button,Modal} from 'react-bootstrap';  
  4. class App extends React.Component {  
  5.   constructor(){  
  6.     super();  
  7.     this.state={  
  8.       show:true,  
  9.       buttonName:"Hide Message"  
  10.     }  
  11.   }  
  13.   render(){  
  14.     return (  
  15.       <div>  
  16.          <div class="col-12 col-md-12">  
  17.         <div class="card">  
  18.           <div class="card-header">  
  19.             Example of hide and show          
  20.             <a onClick={()=>{this.setState({show:!this.state.show})}} class="btn btn-primary"><i  
  21.                 className='fa fa-minus-square-o'></i> {this.state.show ? this.state.buttonName:'Show Message'}</a>  
  23.           </div>  
  24.           {  
  25.             this.state.show ?  
  26.             <div>  
  27.               <p><b>What is Lorem Ipsum?</b><br/>  
  28.                 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>  
  29.           </div>  
  30.           :  
  31.           null  
  32.           }  
  34.             </div>  
  35.           </div>  
  36.         </div>  
  37.     )  
  38.   }  
  39. }  
  40. export default App;  


In this article, we have seen hide and show a DOM element in ReactJS.
Please give your valuable feedback/comments/questions about this article.