Skip to content
theLoneCoder text logo

WHY HAVE FRIENDS WHEN YOU HAVE CODE

  • Home
  • JavaScript
    • React.js
    • Node.js
    • DOM
  • Other
    • Bash/Terminal
    • Resources and Reviews
  • About Me
  • Contact Me
  • Home
  • JavaScript
  • React.js
  • How to Show and Hide React Components (Easy!)
How to show and hide React components

How to Show and Hide React Components (Easy!)

Posted on April 24, 2023May 30, 2023 By theLoneCoder No Comments on How to Show and Hide React Components (Easy!)
React.js

It’s common to want to show and hide React components based on a condition.

For example, imagine you have a pop-up dialog box on your web app. Most likely, you want the box to show up whenever the user has done something such as clicking a button or the like. In order to show and hide the dialog box component, we need three things:

KEEP UP WITH { theLoneCoder }
Thank you for subscribing.
Something went wrong.
I upload new articles and tutorials daily throughout the week, so stay updated! You'll receive new tutorials on React, Node, and more.
We hate spam. Your email address will not be sold or shared with anyone else.
  1. A Boolean state variable to keep track of the component’s visibility status
  2. Some logic to render the component based on that condition
  3. A way to toggle the visibility status on and off

Let’s get to work on it!

NOTE: You can view a live demo of this tutorial on CodeSandbox! I have, of course, added styles and what-not to the demo, but you can see the concepts in action and even edit the code if you like!

And for more cool ways to toggle React components, check out this video by Code Angle!

Setting the `visible` state

To be able to show or hide a component, first we need a state to track the component’s status. This will be a Boolean state, and in most cases, it will default to false.

Here, you can assume that we have a DialogBox component defined in another file. The Container component will manage the dialog box’s visibility.


import { useState } from 'react';
import { DialogBox } from './components/DialogBox';

const Container = () => {
	const [dialogBoxVisible, setDialogBoxVisible] = useState(false);
};

Using a short-circuit to show and hide a component

Now that we have a way to track the component’s visibility status, we need a way to show or hide it based on that status.

We can use some short-circuiting to conditionally render the component based on the Boolean state variable. This will take the form of a Boolean AND operator that returns the component if the variable is equal to true


import { useState } from 'react';
import { DialogBox } from './components/DialogBox';

const Container = () => {
	const [dialogBoxVisible, setDialogBoxVisible] = useState(false);

	return (
		<div id='container'>
			{dialogBoxVisible && <DialogBox />}
		</div>
	);
};

Updating the visibility status with a button

As said earlier, we most likely want this dialog box to appear when the use clicks on a button. So let’s set up a couple of click events. There will be two click events here:

  1. A click event on a button that sets the visibility status to true.
  2. A click event on the entire Container component that sets the status to false. The user just has to click anywhere on the page to get rid of the dialog box.

import { useState } from 'react';
import { DialogBox } from './components/DialogBox';

const Container = () => {
	const [dialogBoxVisible, setDialogBoxVisible] = useState(false);

	const handleButtonClick = (e) => {
e.stopPropagation(); setDialogBoxVisible(true); }; const handleContainerClick = (e) => { setDialogBoxVisible(false); }; return ( <div id='container' onClick={handleContainerClick}> {dialogBoxVisible && <DialogBox />} <button id='showDialog' onClick={handleButtonClick}>Show Dialog Box</button> </div> ); };

Remember, you can see a live demo of this on CodeSandbox!

Check out these articles!

Tags: class-based components functional components JSX

Post navigation

❮ Previous Post: How to Write Comments in React/JSX
Next Post: [FIXED!] ReactDOM Deprecation notice | ReactDOM.render vs. createRoot ❯

You may also like

React.js
5 Most Important React Skills to Get a Job
January 16, 2023
React Tutorial for Beginners: Introduction to React.js
React.js
Introduction to React.js | React Tutorial for Beginners
January 31, 2023
JavaScript
React Flux Architecture: the Foundation of Redux
January 23, 2023
Deprecation notice: ReactDOM.render is no longer supported in React 18. Use createRoot instead
React.js
[FIXED!] ReactDOM Deprecation notice | ReactDOM.render vs. createRoot
April 25, 2023

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • YouTube
  • GitHub
  • CodePen
  • Twitter
Substack

Subscribe to theLoneCoder!

Get updates on all new tutorials and blog posts from theLoneCoder.

Thank you!

You have successfully joined our subscriber list.

Recent Posts

  • [FIXED!] apt upgrade: the following packages have been kept back
  • [FIXED!] ReactDOM Deprecation notice | ReactDOM.render vs. createRoot
  • How to Show and Hide React Components (Easy!)
  • How to Write Comments in React/JSX
  • The React useEffect Hook and How to Use it

Categories

  • Bash/Terminal
  • DOM
  • JavaScript
  • Node.js
  • Other
  • React.js
  • Resources and Reviews
  • Home
  • Privacy Policy
  • About Me
  • Contact Me
  • YouTube

Copyright © 2023 theLoneCoder.

Theme: Oceanly News Dark by ScriptsTown