# Passing state from a toggle button to hide a Div

**URL:** <https://forum.freecodecamp.org/t/passing-state-from-a-toggle-button-to-hide-a-div/338833>\
**Category:** General\
**Created:** [January 6, 2020, 2:20pm UTC](https://forum.freecodecamp.org/t/passing-state-from-a-toggle-button-to-hide-a-div/338833 "2020-01-06T14:20:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![john1](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@john1](https://forum.freecodecamp.org/u/john1)\
**Post date:** [January 6, 2020, 2:20pm UTC](https://forum.freecodecamp.org/t/passing-state-from-a-toggle-button-to-hide-a-div/338833/1 "2020-01-06T14:20:14Z")

</div>

Hi folks,

In a React project, I have a toggle button that shows and hides some info to the user. Now I want to connect a third part to the toggle.

For instance, a div is shown on when the page loads, then the toggle button is clicked then I want that to hide.

I need to know, how do I pass the state from one component to another to show visibility.

https://repl.it/repls/AcceptableLoyalApplicationsuite?lite=true

How do I pass the state to know to hide / show based on users clicking.

Cheers,

John.

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [January 6, 2020, 2:45pm UTC](https://forum.freecodecamp.org/t/passing-state-from-a-toggle-button-to-hide-a-div/338833/2 "2020-01-06T14:45:31Z")

</div>

You don’t pass the state from one to another, you write a component that contains the state that is a parent of both. The class is irrelevant here, you just don’t render the text when it shouldn’t be visible[1](#note-1). Example:

```auto
const Details = () => {
  const [isVisible, setIsVisible] = React.useState(false);

  return (
    <div>
      { isVisible && <p>So this needs to be hidden when the show details is click - how does one pass that state to this component so it knows to hide.</p> }
      <button onClick={() => setIsVisible(!isVisible)}>Show Details</button>
    </div>
</>)
};

```

* * *

**1.** using a class or style prop is useful if you want to animate, but in that case you always render the element but change the class/style based on it being visible or not. It's exactly the same process though.

---

<div class="post-metadata">

**Author:** ![john1](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@john1](https://forum.freecodecamp.org/u/john1)\
**Post date:** [January 6, 2020, 4:59pm UTC](https://forum.freecodecamp.org/t/passing-state-from-a-toggle-button-to-hide-a-div/338833/3 "2020-01-06T16:59:05Z")

</div>

Thank you very much Dan!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 7:38pm UTC](https://forum.freecodecamp.org/t/passing-state-from-a-toggle-button-to-hide-a-div/338833/4 "2021-06-01T19:38:30Z")

</div>

Closed on June 1, 2021, 7:38pm UTC


