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
- Create a boolean state (
true/false) - Display content only when state is true
- Toggle state on button click
- 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 visiblesetShow(!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
How to Master Full Stack Development?
Spring Framework Practice Problems and Their Solutions
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
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
