ReactJS – 从切换按钮传递状态以隐藏一个Div

huangapple go评论67阅读模式
英文:

ReactJS - Passing state from a toggle button to hide a Div

问题

在一个React项目中,我有一个切换按钮,用于向用户显示和隐藏一些信息。现在我想要连接一个第三方组件到这个切换按钮。

例如,当页面加载时,一个div被显示,然后点击切换按钮后,我希望它隐藏。

我需要知道,如何将一个组件的状态传递给另一个组件以显示或隐藏。

我在这里创建了一个REPL:

https://repl.it/repls/AcceptableLoyalApplicationsuite

如何传递状态以便根据用户的点击来隐藏/显示?

干杯!

编辑:

所以我需要检查状态来显示/隐藏组件。

使用if语句或状态检查来显示/隐藏
<Component />
英文:

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.

I have created a REPL here:

https://repl.it/repls/AcceptableLoyalApplicationsuite

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

Cheers!

Edit:

So I need to check the state to show/hide the component.

if statement or state check to show / hide 
&lt;Component /&gt;

答案1

得分: 1

你可以在父组件中像下面这样定义状态和函数,并传递给子组件。

只需更改这两个组件:

App.js

class App extends React.Component {
  state = {
    visibility: false
  };
  handleToggleVisibility = () => {
    this.setState(prevState => ({ visibility: !prevState.visibility }));
  };
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <Test />
          <Button
            visibility={this.state.visibility}
            handleToggleVisibility={this.handleToggleVisibility}
          />
        </header>
      </div>
    );
  }
}

Button.js

export default class Button extends Component {
  render() {
    return (
      <div>
        <em>I have another Div with the className of iNeedToHide</em>
        <br />
        <button onClick={this.props.handleToggleVisibility}>
          {this.props.visibility ? "Hide details" : "Show details"}
        </button>
        {this.props.visibility && (
          <div>
            <p>
              这些是一些你现在可以看到的细节当你看到我时上面的名为iNeedToHide的div需要被隐藏但是怎么做?!如何传递这个状态呢
            </p>
          </div>
        )}
      </div>
    );
  }
}

你的工作代码的实时演示 Live

英文:

You can define state & function in parent component like mentioned below and pass to child components.

Just change these two components

App.js

class App extends React.Component {
  state = {
    visibility: false
  };
  handleToggleVisibility = () =&gt; {
    this.setState(prevState =&gt; ({ visibility: !prevState.visibility }));
  };
  render() {
    return (
      &lt;div className=&quot;App&quot;&gt;
        &lt;header className=&quot;App-header&quot;&gt;
          &lt;Test /&gt;
          &lt;Button
            visibility={this.state.visibility}
            handleToggleVisibility={this.handleToggleVisibility}
          /&gt;
        &lt;/header&gt;
      &lt;/div&gt;
    );
  }
}

Button.js

 export default class Button extends Component {
  render() {
    return (
      &lt;div&gt;
        &lt;em&gt;I have another Div with the className of iNeedToHide&lt;/em&gt;
        &lt;br /&gt;
        &lt;button onClick={this.props.handleToggleVisibility}&gt;
          {this.props.visibility ? &quot;Hide details&quot; : &quot;Show details&quot;}
        &lt;/button&gt;
        {this.props.visibility &amp;&amp; (
          &lt;div&gt;
            &lt;p&gt;
              Hey. These are some details you can now see! When you see me the
              div above called iNeedToHide needs to be hidden! But how?! How do
              I pass that state?!
            &lt;/p&gt;
          &lt;/div&gt;
        )}
      &lt;/div&gt;
    );
  }
}

Live demo of your working code Live

huangapple
  • 本文由 发表于 2020年1月6日 22:34:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定