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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
CSS align-items property examplesCSSThe :last-of-type selectorCSSHow to modify navigation menus in WordPress?WordPressWordPress: How to get ACF field values from another post?WordPressAccessing and Setting features of JavaScript ObjectsJavaScriptWhat are Web services?MiscellaneousAvoid using new Array() in JavaScriptJavaScriptThe fundamental difference between HAVING and WHERE clauses in PostgresPostgresThe basics of @import rule in SCSS (Sass)SCSSHow to create a sidebar using pure CSS?CSSHow to add multiple Columns in CSS like Bootstrap?CSSHow to include CSS and JavaScript files in your WordPress plugin?WordPress