React Toggle Visibility using useState Hook (Show/Hide Example)

๐Ÿ“Œ INTRODUCTION

In React, controlling what is visible on the screen is a common requirement. Using the useState hook, we can easily show or hide content dynamically.

In this program, we will:

  • Toggle visibility of content
  • Use state to control UI
  • Handle button click events

๐Ÿ‘‰ This concept is widely used in modals, dropdowns, menus, and dashboards.


๐Ÿ“˜ PROBLEM STATEMENT

๐Ÿ‘‰ Develop a program to toggle the visibility of content using state in React.


๐Ÿ’ก CONCEPTS USED

  • useState Hook
  • Conditional Rendering
  • Event Handling
  • JSX

๐Ÿง  LOGIC EXPLANATION

  1. Create a boolean state (true/false)
  2. Display content only when state is true
  3. Toggle state on button click
  4. UI updates automatically

๐Ÿ’ป PROGRAM CODE

Open src/App.js:

import { useState } from "react";

function App() {
const [show, setShow] = useState(true);

return (
<div style={{ textAlign: "center", marginTop: "50px" }}>
<h1>Toggle Visibility</h1>

<button onClick={() => setShow(!show)}>
{show ? "Hide" : "Show"}
</button>

{show && <p>This content can be hidden or shown.</p>}
</div>
);
}

export default App;

๐Ÿ–ฅ๏ธ OUTPUT

๐Ÿ‘‰ Initially:

Toggle Visibility
[Hide Button]
This content can be hidden or shown.

๐Ÿ‘‰ After clicking button:

[Show Button]
(Content disappears)

๐Ÿ” STEP-BY-STEP EXPLANATION

  • useState(true) โ†’ initial state is visible
  • setShow(!show) โ†’ toggles value
  • {show && ...} โ†’ conditional rendering
  • Button text changes dynamically

โšก ALTERNATIVE METHOD (Using if-else)

{show ? <p>Visible Content</p> : null}

๐ŸŽฏ KEY CONCEPT: CONDITIONAL RENDERING

React allows you to display elements based on conditions:

  • && operator โ†’ simple conditions
  • ? : โ†’ if-else logic

๐ŸŒŸ ADVANTAGES

  • Simple UI control
  • Efficient rendering
  • Improves user experience
  • Used in almost every React app

๐Ÿ“š REAL-WORLD USE CASE

  • Show/hide password fields
  • Toggle menus and sidebars
  • Display modals/popups
  • Expand/collapse sections

โ“ VIVA QUESTIONS (IMPORTANT)

Q1. What is useState used for?

๐Ÿ‘‰ To manage state in functional components.


Q2. What is conditional rendering?

๐Ÿ‘‰ Displaying UI based on conditions.


Q3. What does && operator do in React?

๐Ÿ‘‰ Renders content only if condition is true.


Q4. What is boolean state?

๐Ÿ‘‰ A state with true/false values.


Q5. Can we toggle UI without useState?

๐Ÿ‘‰ No, state is required for dynamic updates.


๐Ÿ”— RELATED POSTS

๐Ÿ‘‰ MCA 168 Full Stack Development Lab Programs List
๐Ÿ‘‰ React Counter App โ€“ Program 6
๐Ÿ‘‰ React List Rendering โ€“ Program 7
๐Ÿ‘‰ React Hello World โ€“ Program 5


๐Ÿ“Œ CONCLUSION

Toggle functionality is a fundamental concept in React. It helps in building interactive and dynamic user interfaces.

๐Ÿ‘‰ Practice this concept โ€” it is widely used in real-world applications.


Further Reading

JUnit Tutorial

How to Master Full Stack Development?

Spring Framework Practice Problems and Their Solutions

30 MCQs on JUnit

From Google to the World: The Story of Go Programming Language

Why Go? Understanding the Advantages of this Emerging Language

Creating and Executing Simple Programs in Go

20+ Interview Questions on Go Programming Language

100+ MCQs On Java Architecture

Java Practice Exercise

programmingempire

Princites

Leave a Reply

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