简体   繁体   English

父状态更改时,子组件未更新

[英]Child component is not updating when parent state changes

I have a parent component with an array in its state, the array is added too over time. 我有一个处于阵列状态的父组件,该阵列也会随着时间的推移而添加。

This is the parent. 这是父母。 There is a lot going on in it but I've distilled it down to the main concerns the state, componentShouldUpdate function (which works for everything else the component needs to do) and the show data method which also works (I can confirm that gaitStats does update properly). 里面有很多事情,但是我将其简化为主要的状态,即componentShouldUpdate函数(对组件需要执行的其他所有工作)和show data方法也可以工作(我可以确认gaitStats确实正确更新)。

  class GaitMeasure extends Component {
  constructor(props) {
    super(props);

    this.state = {
      grabData: null;
      gaitStats: []
    };
  }  
  shouldComponentUpdate(nextProps) {
   if (this.props.data !== null) {
     if (this.props.data !== nextProps.data) {
       this.showData();
    }
      return false;
    }
   if (this.props.canvas.x !== null) {
     return true;
   } 
  if (this.state.grabData) {
    return true;
  }

  return false;
}

showData() {
....

const avgGait = [...this.state.gaitStats];

avgGait.push(Math.abs(rightX - leftX));

this.setState({
  timeline: this.state.timeline + 3,
  gaitStats: avgGait
});

// render

GaitStat, child: GaitStat,孩子:

class GaitStat extends Component {
  constructor(props) {
    super(props);
  }


  render() {
    return (
      <div>
        Hi
        <p>{this.props.gaitStats}</p>
      </div>
    );
  }
}

From what I can tell GaitStat doesn't ever rerender this.state.gaitStatus as a prop after the initial render. 据我所知,在初始渲染之后, this.state.gaitStatus永远不会将this.state.gaitStatus渲染为道具。 The prop remains an empty array regardless of how many times the parent rerenders or updates it's state. 无论父对象重新渲染或更新其状态多少次,该prop仍为空数组。

Your shouldComponentUpdate method prevents the class from updating when it's state changes. 您的shouldComponentUpdate方法可防止类在状态更改时进行更新。 You should update you shouldComponentUpdate method to the folowing 您应该将您的shouldComponentUpdate方法更新为以下内容

shouldComponentUpdate(nextProps, nextState) {
   if(nextState !== this.state){
        return true;
   }
   if (this.props.data !== null) {
     if (this.props.data !== nextProps.data) {
       this.showData();
    }
      return false;
    }
   if (this.props.canvas.x !== null) {
     return true;
   } 
   if (this.state.grabData) {
     return true;
   }

  return false;
}

You can find more information about shouldComponentUpdate here 您可以在此处找到有关shouldComponentUpdate的更多信息。

you can try with in your child component 您可以在子组件中尝试

shouldComponentUpdate(nextProps, nextState) { 
  if(nextProps.gaitStats != this.props.gaitStatus) {
   this.forceUpdate();
 }
}

try this: 尝试这个:

 shouldComponentUpdate(nextProps) {
   if ((this.props.data === null && this.props.data !== nextProps.data) || this.props.canvas.x !== null || this.state.grabData) {
        return true;
      }
  this.showData();
  return false;
}

this includes all your condition to render in one if and if noting meet the criteria then run showData and return false eg don't update 这包括您所有要渲染的条件,如果满足条件则运行showData并返回false,例如不更新

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

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