Yogesh Chauhan's Blog

How to show and hide an element on click in React?

in React on May 7, 2021

Just return null instead of the rendered output to hide a component.

You can hide the component even when it’s being rendered using another component.

Something like this:


...
  if (!props.content) {
    return null;
  }
...

Render the element like the way you usually do:



...
   <Content content={this.state.showContent} />
...


Here’s the working example:

This is really useful with conditional rendering.


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to display a student’s individual transcript in Colleague?ColleagueWhy does MOV matter in IT or any kind of projects?MiscellaneousWhat are “holes” in a React component?ReactThe Sort and Compare Functions in JavaScriptJavaScriptHow to use [] (square brackets) function in Envision Basic?Envision BasicWordPress: How to get field values in Advanced Custom Fields?WordPressUPDATE and DELETE Statements in PostgresPostgresWordPress: How to change a blog address (URL)?WordPressaddcslashes() and addslashes() String Functions in PHPPHPHow to reference an aliased column in the WHERE clause?SQL/MySQLHow to add AppRoutingModule in Angular application using command line?AngularWhat are components in Angular?Angular