How to toggle boolean state of react component?

前端 未结 9 1723
别跟我提以往
别跟我提以往 2021-01-29 23:45

I\'d like to know how to toggle a boolean state of a react component. For instance:

I have boolean state check in the constructor of my component:

const         


        
相关标签:
9条回答
  • 2021-01-30 00:19

    Try:

    <label><input type=checkbox" value="check" onChange = {(e) => this.setState({check: !this.state.check.value})}/> Checkbox </label>
    

    Using check: !check.value means it is looking for the check object, which you haven't declared.

    You need to specify that you want the opposite value of this.state.check.

    0 讨论(0)
  • 2021-01-30 00:21

    You should use this.state.check instead of check.value here:

    this.setState({check: !this.state.check})
    

    But anyway it is bad practice to do it this way. Much better to move it to separate method and don't write callbacks directly in markup.

    0 讨论(0)
  • 2021-01-30 00:23

    Use checked to get the value. During onChange, checked will be true and it will be a type of boolean.

    Hope this helps!

    class A extends React.Component {
      constructor() {
        super()
        this.handleCheckBox = this.handleCheckBox.bind(this)
        this.state = {
          checked: false
        }
      }
      
      handleCheckBox(e) {
        this.setState({
          checked: e.target.checked
        })
      }
      
      render(){
        return <input type="checkbox" onChange={this.handleCheckBox} checked={this.state.checked} />
      }
    }
    
    ReactDOM.render(<A/>, document.getElementById('app'))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    <div id="app"></div>

    0 讨论(0)
  • 2021-01-30 00:23

    Depending on your context; this will allow you to update state given the mouseEnter function. Either way, by setting a state value to either true:false you can update that state value given any function by setting it to the opposing value with !this.state.variable

    state = {
      hover: false
    }
    
    onMouseEnter = () => {
      this.setState({
        hover: !this.state.hover
      });
    };
    
    0 讨论(0)
  • 2021-01-30 00:24

    I found this the most simple when toggling boolean values. Simply put if the value is already true then it sets it to false and vice versa. Beware of undefined errors, make sure your property was defined before executing

    this.setState({
       propertyName: this.propertyName = !this.propertyName
    });
    
    0 讨论(0)
  • 2021-01-30 00:28

    Here's an example using hooks (requires React >= 16.8.0)

    // import React, { useState } from 'react';
    const { useState } = React;
    
    function App() {
      const [checked, setChecked] = useState(false);
      const toggleChecked = () => setChecked(value => !value);
      return (
        <input
          type="checkbox"
          checked={checked}
          onChange={toggleChecked}
        />
      );
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    
    <div id="root"><div>

    0 讨论(0)
提交回复
热议问题