Open In App

How to Show or Hide Element in React ?

Last Updated : 03 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

We can show or hide element in React dynamically by accessing the visibility of the elements with the help of state. We can toggle and update the state and render the variable as required.

Approach

To Show or Hide Element in React we will be using the state variable as a boolean value. We will conditionally render the component based on its value i.e. true or false. We will use a button to toggle the state from true to false and vice-versa.

We have different ways to implement the state in class and functional components

Steps to Create React Application

Step 1: Create a React application using the following command:

npx create-react-app foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd foldername

Project Structure:

project-structure---Show-or-Hide-Element-in-React

List of dependencies in the package.json file are:

"dependencies": {
    "@testing-library/jest-dom": "^5.17.0",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "react-scripts": "5.0.1",
    "web-vitals": "^2.1.4"
},

As we discussed above we have to create few components and render them into a single parent file to execute the conditions so that we can apply the functionality of showing or hiding to some particular components. Let's create three child components and name them Child1.js, Child2.js, and Child3.js in the src folder and paste the following code into respective files.

using Class Components

In case of class components we can directly access state using this.state and modify it with the help of this.setState method.

Example: This example use the state in class components to store the boolean value to conditiona;lly render the element.

JavaScript
// Filename - App.js

import React, { Component } from "react";
import { render } from "react-dom";
import Child1 from "./Child1";
import Child2 from "./Child2";
import Child3 from "./Child3";

class App extends Component {
	constructor() {
		super();
		this.state = {
			name: "React",
			shchild1: false,
			shchild2: false,
			shchild3: false
		};
		this.hideComponent = this.hideComponent.bind(this);
	}

	hideComponent(varname) {
		console.log(varname);
		switch (varname) {
			case "shchild1":
				this.setState({ shchild1: !this.state.shchild1 });
				break;
			case "shchild2":
				this.setState({ shchild2: !this.state.shchild2 });
				break;
			case "shchild3":
				this.setState({ shchild3: !this.state.shchild3 });
				break;
			default:
				return;
		}
	}

	render() {
		const { shchild1, shchild2, shchild3 } = this.state;
		return (
			<div>
				{shchild1 && <Child1 />}
				<hr />
				{shchild2 && <Child2 />}
				<hr />
				{shchild3 && <Child3 />}
				<hr />
				<div>
					<button onClick={() => this.hideComponent("shchild1")}>
						Click here to hide GFG child1 component
					</button>
					<button onClick={() => this.hideComponent("shchild2")}>
						Click here to hide GFG child2 component
					</button>
					<button onClick={() => this.hideComponent("shchild3")}>
						Click here to hide GFG child3 component
					</button>
				</div>
			</div>
		);
	}
}

export default App;
JavaScript
// Filename - Child1.js

import React, { Component } from "react";

class Child1 extends Component {
	constructor() {
		super();
		this.state = {
			name: "React"
		};
	}

	render() {
		return <div>This is how GFG child component number 1 looks like.</div>;
	}
}

export default Child1;
JavaScript
// Filename - Child2.js

import React, { Component } from "react";

class Child2 extends Component {
	constructor() {
		super();
		this.state = {
			name: "React"
		};
	}

	render() {
		return <div>This is how GFG child component number 2 looks like.</div>;
	}
}

export default Child2;
JavaScript
// Filename - Child3.js

import React, { Component } from "react";

class Child3 extends Component {
	constructor() {
		super();
		this.state = {
			name: "React"
		};
	}

	render() {
		return <div>This is how GFG child component number 3 looks like.</div>;
	}
}

export default Child3;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output:  

output---show-or-hide-element-in-React

using Functional Components

In case of functional components we cannot access state directly. We use the useState hook to access the state in functional components.

Example: This example implements show and hide element in react with the help of useState variable to store the boolean value and toggle the value with help of buttons.

JavaScript
// Filename - App.js

import React, { useState } from "react";
import Child1 from "./Child1";
import Child2 from "./Child2";
import Child3 from "./Child3";

const App = () => {
	const [shchild1, setShchild1] = useState(false);
	const [shchild2, setShchild2] = useState(false);
	const [shchild3, setShchild3] = useState(false);

	const hideComponent = (varname) => {
		switch (varname) {
			case "shchild1":
				setShchild1(!shchild1);
				break;
			case "shchild2":
				setShchild2(!shchild2);
				break;
			case "shchild3":
				setShchild3(!shchild3);
				break;
			default:
				return;
		}
	};

	return (
		<div
			style={{
				width: "50%",
				textAlign: "center",
				margin: "auto",
				marginTop: "50px"
			}}
		>
			{shchild1 && <Child1 />}
			<hr />
			{shchild2 && <Child2 />}
			<hr />
			{shchild3 && <Child3 />}
			<hr />
			<div>
				<button onClick={() => hideComponent("shchild1")}>
					Click here to hide GFG child1 component
				</button>
				<button onClick={() => hideComponent("shchild2")}>
					Click here to hide GFG child2 component
				</button>
				<button onClick={() => hideComponent("shchild3")}>
					Click here to hide GFG child3 component
				</button>
			</div>
		</div>
	);
};

export default App;
JavaScript
// Filename - Child1.js

import React from "react";

const Child1 = () => {
	return <div>This is how GFG child component number 1 looks like.</div>;
};

export default Child1;
JavaScript
// Filename - Child2.js

import React from "react";

const Child2 = () => {
	return <div>This is how GFG child component number 2 looks like.</div>;
};

export default Child2;
JavaScript
// Filename - Child3.js

import React from "react";

const Child3 = () => {
	return <div>This is how GFG child component number 3 looks like.</div>;
};

export default Child3;

Output:

output---show-or-hide-element-in-React

Next Article

Similar Reads