javascript - ReactJS - Checkbox onChange event not firing - Stack Overflow

I'm trying to implement a fields set of checkboxes in React rendered from an object as follows:con

I'm trying to implement a fields set of checkboxes in React rendered from an object as follows:

constructor() {

    this.state.todo = {
        eat: true,
        sleep: false,
        react: true
    }

    this.toggleCheckbox = this.toggleCheckbox.bind(this);
}

toggleCheckbox(e){

    console.log(e); // nothing :-/
}

render() {

    return (
    <div>
    { Object.keys(this.state.todo).map((val, i) => (          
        <div key={i} >
            <input 
                type="checkbox" 
                value={val} 
                onChange={this.toggleCheckbox} 
                checked={this.state.todo[val]} 
                /><label>{val}</label>
        </div>
     ))}
     </div>
    )
}

Everything renders correctly but I am not able change any of the checkboxes. console logging the toggleCheck() event is not being triggered.

Ive tried using onClick vs onChange which has no effect.

I'm trying to implement a fields set of checkboxes in React rendered from an object as follows:

constructor() {

    this.state.todo = {
        eat: true,
        sleep: false,
        react: true
    }

    this.toggleCheckbox = this.toggleCheckbox.bind(this);
}

toggleCheckbox(e){

    console.log(e); // nothing :-/
}

render() {

    return (
    <div>
    { Object.keys(this.state.todo).map((val, i) => (          
        <div key={i} >
            <input 
                type="checkbox" 
                value={val} 
                onChange={this.toggleCheckbox} 
                checked={this.state.todo[val]} 
                /><label>{val}</label>
        </div>
     ))}
     </div>
    )
}

Everything renders correctly but I am not able change any of the checkboxes. console logging the toggleCheck() event is not being triggered.

Ive tried using onClick vs onChange which has no effect.

Share Improve this question edited Aug 6, 2018 at 3:35 yevg asked Aug 6, 2018 at 3:13 yevgyevg 1,97611 gold badges36 silver badges77 bronze badges 1
  • 1 actually the problem as in CSS, not JS. these checkboxes are custom styled using <label>'s and the hiding/placement of the label was blocking the event. apologies again about the false alarm – yevg Commented Aug 9, 2018 at 17:05
Add a ment  | 

1 Answer 1

Reset to default 2

You are getting the keys from this.state.tables, but your state is called this.state.todo.

You can use each value as name instead and toggle the relevant todo state property with that.

Example

class App extends React.Component {
  state = {
    todo: {
      eat: true,
      sleep: false,
      react: true
    }
  };

  toggleCheckbox = e => {
    const { name } = e.target;
    this.setState(prevState => ({
      todo: {
        ...prevState.todo,
        [name]: !prevState.todo[name]
      }
    }));
  };

  render() {
    return (
      <div>
        {Object.keys(this.state.todo).map((val, i) => (
          <div key={i}>
            <input
              type="checkbox"
              name={val}
              onChange={this.toggleCheckbox}
              checked={this.state.todo[val]}
            />
            <label>{val}</label>
          </div>
        ))}
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="root"></div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745156042a4614126.html

相关推荐

  • javascript - ReactJS - Checkbox onChange event not firing - Stack Overflow

    I'm trying to implement a fields set of checkboxes in React rendered from an object as follows:con

    10小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信