简体   繁体   English

为什么我的组件 state 只更新一次

[英]Why is my component state updating only once

i think it is not soo big problem but i don't understand why is it happening like that.我认为这不是什么大问题,但我不明白为什么会这样。 I have Component structure like this: Parrent->Child1->Child2我有这样的组件结构:Parrent->Child1->Child2

In parrent i have method to setState of count checked checkbox in Child2.在父母中,我有方法在 Child2 中设置计数复选框。 It looks like this:它看起来像这样:

  setKnowledgeCountState(value) {
        this.setState({countChooseKnowledge: this.state.countChooseKnowledge + value});
    }

This method i am passing throught Child1 to Child2.这个方法我通过 Child1 到 Child2。 In Child2 i am using this method like this:在 Child2 我使用这种方法是这样的:


    componentDidUpdate(prevProps, prevState, snapshot) {
        if(this.props.knowledge.isChecked !== prevProps.knowledge.isChecked){
        console.log(prevProps.knowledge.isChecked);
        console.log("---");
        console.log(this.props.knowledge.isChecked);
        this.updateCountOfSelected(this.props.knowledge.isChecked);
        }
    }

  updateCountOfSelected(isChecked){
        if(isChecked)
            this.props.setKnowledgeCountState(1);
        else
            this.props.setKnowledgeCountState(-1);
    }

This works as i want separatly, when i am clicking on checkboxes one by one.当我一个接一个地单击复选框时,这可以按我的意愿单独工作。 But in Child1 i have option to select all checkboxes.但是在 Child1 中,我可以选择 select 所有复选框。 I am doing it like this:我这样做是这样的:

  toggleCheckBox(event) {
        event.stopPropagation();
        const themePartCopy = cloneDeep(this.props.part);
        themePartCopy.isChecked = !themePartCopy.isChecked;
        themePartCopy.isOpen = themePartCopy.isChecked;
        themePartCopy.knowledges.forEach((knowledge) => {
            knowledge.isChecked = themePartCopy.isChecked;
        });

        this.props.changePart(themePartCopy);
    }

Now the problem is, when i select all checkboxes (in my case there are 4xChild2 under Child1) the state in Parrent is updating only once.现在的问题是,当我 select 所有复选框(在我的情况下 Child1 下有 4xChild2)时,Parrent 中的 state 仅更新一次。 So count at the end is 1. It should be 4.I checked console.logs() in child2 and looks like the method updateCountofsELECTED() is called 4x.所以最后的计数是 1。应该是 4。我在 child2 中检查了 console.logs(),看起来方法 updateCountofsELECTED() 被称为 4x。 Why then tha count is only 1 and not 4?那么为什么计数只有 1 而不是 4? Thx for your help谢谢你的帮助

use prevstate while updating the state value.在更新 state 值时使用prevstate React setState is an asynchronous update and does batch updating. React setState是一个异步更新,并进行批量更新。 Using prevState makes sure that the state value is updated before calculating new state value.使用prevState确保在计算新的 state 值之前更新 state 值。

  setKnowledgeCountState = value => {
    this.setState(prevState => {
      return {
        countChooseKnowledge: prevState.countChooseKnowledge + value
      };
    });
  };

DOCUMENTATION would help understand the concept 文档将有助于理解这个概念

React Docs recommends using function with prevState inside of setState React Docs 建议在 setState 中使用带有 prevState 的 function

// Wrong
this.setState({
  counter: this.state.counter + this.props.increment,
});

// es6 way
// Correct
this.setState((prevState, props) => ({
  counter: prevState.counter + props.increment
}));

// es5 way
// Correct
this.setState(function(prevState, props) {
  return {
    counter: prevState.counter + props.increment
  };
});

Source 资源

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

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