🟨 React
Easy
🕐 5 min

Disappearing Button

Goal: Create a component with a button that disappears when clicked by the user.

💡 Solution Hint

To create a disappearing button you will need:

  1. State: Use the useState hook to track button visibility.
  2. Event Handler: Create a function that changes state on click.
  3. Conditional Rendering: Display button or message based on state.
  4. Styling: Add styles to improve appearance (optional).
// Example of using useState
const [isVisible, setIsVisible] = useState(true);
 
// Example event handler
const handleClick = () => {
  setIsVisible(false);
};
👀 Solution
import React, { useState } from 'react';
 
export default function DisappearingButton() {
  // State to track button visibility
  const [isVisible, setIsVisible] = useState(true);
 
  // Click handler that hides the button
  const handleClick = () => {
    setIsVisible(false);
  };
 
  return (
    <div style={{ textAlign: 'center', margin: '20px' }}>
      {isVisible ? (
        <button 
          onClick={handleClick}
          style={{
            padding: '10px 20px',
            backgroundColor: '#4CAF50',
            color: 'white',
            border: 'none',
            borderRadius: '4px',
            cursor: 'pointer',
            fontSize: '16px'
          }}
        >
          Click me!
        </button>
      ) : (
        <p style={{ fontSize: '18px', color: '#f44336' }}>
          Button disappeared! ✨
        </p>
      )}
    </div>
  );
}

Solution Analysis:

  • State: We use the useState hook to create the isVisible state with an initial value of true.
  • Event Handler: The handleClick function changes the state to false when clicked.
  • Conditional Rendering: We use a ternary operator to display the button or message based on state.
  • Styling: Basic styles are added to improve the component’s appearance.
  • UX: After the button disappears, a message informs the user about it.

Task Description

You need to create a component with a button that disappears when clicked by the user.

Key requirements:

  • On initial render, a button should be displayed
  • When the button is clicked, it should disappear
  • After the button disappears, you can display a message (optional)
  • Use the useState hook to manage visibility state

Examples

// On initial render
<DisappearingButton /> // Displays "Click me!" button
 
// After clicking the button
// Displays "Button disappeared! ✨" message

Requirements

  • The component should be named DisappearingButton
  • Use the useState hook to manage visibility state
  • Implement a click event handler
  • Apply conditional rendering to display button or message
  • Component styling is welcome but not required

🧑‍💻 It's not a bug! It's a feature!

The code editor is intentionally hidden on mobile.

Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.

📖 Now: Study the task, think through the solution. Act like a strategist.

💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!