简体   繁体   English

如何在状态中使用切换?

[英]How do I use toggle in react with states?

Having problem to find the solution of using state in React. 在React中找不到使用状态的解决方案。

I've not done it with bind-method because I'd rather to make it work this way with arrow-functions. 我还没有使用bind-method来完成它,因为我希望通过箭头功能使它以这种方式工作。

constructor(props) {
    super(props);

    this.state = {
        isOpen: false
    };
}

toggleNavbar = () => {
    this.setState(prevState => ({
        isOpen: !prevState.isOpen;
    });
}

render() {
    const { isOpen } = this.state;

return (
  <nav className="navbar navbar-expand-lg navbar-light bg-light">
    <a className="navbar-brand mr-4" href="">Do you want to build sowman?</a>
    <button
      className="navbar-toggler"
      type="button"
      data-toggle="collapse"
      data-target="#navbarSupportedContent"
      aria-controls="navbarSupportedContent"
      aria-expanded="false"
      aria-label="Toggle navigation"
      onclick={() => this.toggleNavbar()}>
      <span className="navbar-toggler-icon" />
    </button>
    <div
      className="collapse navbar-collapse"
      id="navbarSupportedContent"
      isOpen={isOpen}>

I want the toggle-button to show/hide the div that's comming ahead.(This is just a sample of the text and won't work on its own) 我希望切换按钮显示/隐藏前面的div(这只是文本的一个示例,不能单独使用)

In order to toggle your div based on state, you can either: 为了根据状态切换div,您可以:

  1. Remove the semicolon ; 删除分号; from your setState property inside toggleNavbar . toggleNavbar内的setState属性中。

  2. React does not allow camelcase writing for attribute names. React不允许使用驼峰式写法来编写属性名称。 Change isOpen to isopen in your #navbarSupportedContent div. 更改isOpenisopen#navbarSupportedContent股利。

  3. Specify a string instead of a boolean as your isopen attribute value and based on that, change the css of the div accordingly. 指定一个字符串而不是布尔值作为您的isopen属性值,并根据该值相应地更改div的css。

You can check out a practical example with css and html of the above in this CodePen or you can check the React class component code in the Code Snippet below: 您可以在此与CSS和上面的HTML检查出一个实际的例子CodePen或者你可以检查下面的代码片段的阵营类组件的代码:

 class App extends React.Component { constructor(props) { super(props); this.state = { isOpen: false }; } toggleNavbar = () => { this.setState(prevState => ({ isOpen: !prevState.isOpen })); } render() { const { isOpen } = this.state; return ( <div> <button onClick={this.toggleNavbar}>Click Me</button> <div id="navbarSupportedContent" isopen={isOpen ? "open" : "close"}> <p>Hello World</p> </div> </div> ) } } ReactDOM.render(<App />, document.getElementById('main')); 


Or you can use the && operator as @ŁukaszBlaszyński showed above by: 或者,您可以将&& operator用作@aboveukaszBlaszyński,如上所示,方法是:

  1. Removing the semicolon ; 删除分号; from your setState property inside toggleNavbar . toggleNavbar内的setState属性中。

  2. Use the && operator to toggle the div depending on the isOpen state property. 使用&& operator可以根据isOpen状态属性来切换div。

You can check out a practical example of the above in this CodePen or you can check the React class component code in the Code Snippet below: 您可以检查出上述的实际例子在这个CodePen或者你可以检查下面的代码片段的阵营类组件的代码:

 class App extends React.Component { constructor(props) { super(props); this.state = { isOpen: false }; } toggleNavbar = () => { this.setState(prevState => ({ isOpen: !prevState.isOpen })); } render() { const { isOpen } = this.state; return ( <div> <button onClick={this.toggleNavbar}>Click Me</button> {isOpen && <div id="navbarSupportedContent" > <p>Hello World</p> </div>} </div> ) } } ReactDOM.render(<App />, document.getElementById('main')); 

Since you are using arrow functions, you just can define your property like : 由于您使用的是箭头功能,因此只需定义属性即可:

onclick={this.toggleNavbar}>

and not making another arrow function inside propery 而不是在属性内制作另一个箭头功能

See this fiddle it works fine: https://jsfiddle.net/y5gqrf02/1/ 看到这个小提琴就可以正常工作: https : //jsfiddle.net/y5gqrf02/1/

Example code which simple toggle state and display message or not based on state. 示例代码可以简单地切换状态并根据状态不显示消息。 It use arrow function and simply take state isOpen negation to be updated. 它使用箭头功能,只需将isOpen否定状态更新即可。

class Hello extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
        isOpen: false
    };
  }

  toggleNavbar() {
      this.setState({
          isOpen: !this.state.isOpen
      });
  }

  render() {
      const { isOpen } = this.state;

      return (
         <div onClick={() => this.toggleNavbar()}>Click Me 
           {isOpen && <div>IS OPEN</div>}
         </div>
      );

  }
};

ReactDOM.render(
  <Hello name="World" />,
  document.getElementById('container')
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM