How to set default Checked in checkbox ReactJS?

前端 未结 16 1823
难免孤独
难免孤独 2020-11-27 11:51

I\'m having trouble to update the checkbox state after it\'s assigned with default value checked="checked" in React.

var rCheck = React         


        
相关标签:
16条回答
  • 2020-11-27 12:27
    If someone wants to handle dynamic data with multiple rows, this is for 
    handing dynamic data. 
    You can check if the rowId is equal to 0.
    If it is equal to 0, then you can set the state of the boolean value as true.
    interface MyCellRendererState {
        value: boolean;
    }
    constructor(props) {
            super(props);
            this.state = {
                value: props.value ? props.value : false
            };
            this.handleCheckboxChange = this.handleCheckboxChange.bind(this);
        }
    handleCheckboxChange() {
            this.setState({ value: !this.state.value });
        };
    render() {
            const { value } = this.state;
            const rowId = this.props.rowIndex
            if (rowId === 0) {
               this.state = {
                 value : true }
            }
            return ( 
              <div onChange={this.handleCheckboxChange}>
                <input 
                type="radio" 
                checked={this.state.value}
                name="print"
                /> 
              </div>
             )
          }
    
    0 讨论(0)
  • 2020-11-27 12:30

    It`s working

    <input type="checkbox" value={props.key} defaultChecked={props.checked} ref={props.key} onChange={this.checkboxHandler} />
    

    And function init it

    {this.viewCheckbox({ key: 'yourKey', text: 'yourText', checked: this.state.yourKey })}
    
    0 讨论(0)
  • 2020-11-27 12:36

    Don't make it too hard. First, understand a simple example given below. It will be clear to you. In this case, just after pressing the checkbox, we will grab the value from the state(initially it's false), change it to other value(initially it's true) & set the state accordingly. If the checkbox is pressed for the second time, it will do the same process again. Grabbing the value (now it's true), change it(to false) & then set the state accordingly(now it's false again. The code is shared below.

    Part 1

    state = {
      verified: false
    } // The verified state is now false
    

    Part 2

    verifiedChange = e => {
      // e.preventDefault(); It's not needed
      const { verified } = e.target;
      this.setState({
        verified: !this.state.verified // It will make the default state value(false) at Part 1 to true 
      });
    }; 
    

    Part 3

      <form> 
          <input
              type="checkbox"
              name="verified"
              id="verified"
              onChange={this.verifiedChange} // Triggers the function in the Part 2
              value={this.state.verified}
          />
          <label for="verified">
          <small>Verified</small>
          </label>
      </form>
    
    0 讨论(0)
  • 2020-11-27 12:37

    In the React rendering lifecycle, the value attribute on form elements will override the value in the DOM. With an uncontrolled component, you often want React to specify the initial value, but leave subsequent updates uncontrolled. To handle this case, you can specify a defaultValue or defaultChecked attribute instead of value.

            <input
              type="checkbox"
              defaultChecked={true}
            />
    

    Or

    React.createElement('input',{type: 'checkbox', defaultChecked: true});
    

    Please checkout more details regarding defaultChecked for checkbox below: https://reactjs.org/docs/uncontrolled-components.html#default-values

    0 讨论(0)
  • 2020-11-27 12:38

    There are a few ways to accomplish this, here's a few:

    Written using State Hooks:

    function Checkbox() {
      const [checked, setChecked] = React.useState(true);
    
      return (
        <label>
          <input type="checkbox"
            checked={checked}
            onChange={() => setChecked(!checked)}
          />
          Check Me!
        </label>
      );
    }
    
    ReactDOM.render(
      <Checkbox />,
      document.getElementById('checkbox'),
    );
    

    Here is a live demo on JSBin.

    Written using Components:

    class Checkbox extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          isChecked: true,
        };
      }
      toggleChange = () => {
        this.setState({
          isChecked: !this.state.isChecked,
        });
      }
      render() {
        return (
          <label>
            <input type="checkbox"
              checked={this.state.isChecked}
              onChange={this.toggleChange}
            />
            Check Me!
          </label>
        );
      }
    }
    
    ReactDOM.render(
      <Checkbox />,
      document.getElementById('checkbox'),
    );
    

    Here is a live demo on JSBin.

    0 讨论(0)
  • 2020-11-27 12:39
     <div className="form-group">
              <div className="checkbox">
                <label><input type="checkbox" value="" onChange={this.handleInputChange.bind(this)}  />Flagged</label>
                <br />
                <label><input type="checkbox" value=""  />Un Flagged</label>
              </div>
            </div
    

    handleInputChange(event){

    console.log("event",event.target.checked)   }
    

    the Above handle give you the value of true or false upon checked or unChecked

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