在React的回调函数中为每个添加多个

时间:2019-04-24 06:53:27

标签: javascript reactjs redux

我是react redux.的新手,在这里我想做的是

我有一个单击复选框便会调用的函数。

handleCheckBox = () => {
    this.setState({
      isCheckd: !this.state.isCheckd,
      isEnable: !this.state.isCheckd ? true : false
    }, () => {
      this.props.untracked.forEach((item) => this.setState({ [item.resumeId]: this.state.isCheckd }))
    });
  }

现在在此函数中,我有一个回调,其中我在具有多个对象的一个​​数组上使用一个foreach循环。

现在,我想像在likem之后一样调用另一个回调函数

 handleCheckBox = () => {
    this.setState({
      isCheckd: !this.state.isCheckd,
      isEnable: !this.state.isCheckd ? true : false
    }, () => {
      this.props.untracked.forEach((item) => this.setState({ [item.resumeId]: this.state.isCheckd })),
this.props.tracked.forEach((item) => this.setState({ [item.resumeId]: this.state.isCheckd }))
    });
  }

像这样

  handleTableCheckboxChange = (e, type) => {
    this.setState({
      [e.target.name]: e.target.checked
    }, () => {
      const checkedItems = this.props[type].filter((item) => this.state[item.resumeId])
      this.setState({
        isCheckd: checkedItems.length === this.props[type].length ? true : false,
        isEnable: checkedItems.length > 1 ? true : false
      });
    });
  }

那么,有什么办法可以做到这一点?感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在forEach末尾删除逗号(,)并放入;

handleCheckBox = () => {
    this.setState({
      isCheckd: !this.state.isCheckd,
      isEnable: !this.state.isCheckd ? true : false
    }, () => {
      //remove the comma at end of the forEach and put ;
      this.props.untracked.forEach((item) => this.setState({ [item.resumeId]: this.state.isCheckd }));
this.props.tracked.forEach((item) => this.setState({ [item.resumeId]: this.state.isCheckd }));
    });
  }