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
Mastering React

Challenge: Toggling VisibilityChallenge: 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.

Все було зрозуміло?

Секція 3. Розділ 3
course content

Зміст курсу

Mastering React

Challenge: Toggling VisibilityChallenge: 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.

Все було зрозуміло?

Секція 3. Розділ 3
some-alt