Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Toggling Visibility | React Hooks and Context
React Mastery
course content

Course Content

React Mastery

React Mastery

1. Introduction to React Fundamentals
2. Styling in React Apps
3. React Hooks and Context
4. React in the Real World

bookChallenge: Toggling Visibility

Task

Create a component that includes a button and a text paragraph. Implement the necessary logic using the useState hook to toggle the visibility of the text paragraph when the button is clicked.

Instructions

  1. Import the useState hook from the React library.
  2. Declare a state variable named isVisible using the useState hook. Initialize it with the value false.
  3. Implement a function named toggleVisibility that, when called, toggles the isVisible state between true and false.
  4. Use the toggleVisibility function as the onClick handler for the button.
  1. Include an import statement to import the appropriate hook from the React library.
  2. For this task, we will utilize the useState hook as we are managing the state of paragraph visibility.
  3. To determine the appropriate variable name for the state, observe the function associated with setting the state, which is setIsVisible. Remove the "set" prefix and use a lowercase first letter for the variable name, which should be isVisible.
  4. To invoke the toggleVisibility function when the button is clicked, assign it as the value for the onClick attribute of the button.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt