我有一个使用redux存储的react应用程序,该组件也使用redux-form . 我在一个特定类型的更新上看到一个奇怪的行为,我想触发一个后续更新 .

基本上用户正在更新表单上的某些内容的权限,然后保存 . 保存提交表单并在处理操作时触发另一个操作(获取所有事件的列表) . 从服务器返回列表时,将调度操作以更新列表UI . 此时我正在更新表单的redux存储中的属性,以触发更新当前编辑事物的权限 . 这是我能做到这一点的唯一一点,因为我们现在有了触发此请求的信息 .

问题是布尔标志被设置为触发器,并且使用更新的标志调用mapStateToProps,但随后不会调用该组件(shouldComponentUpdate和componentWillUpdate永远不会被调用) . 它就像忽略了props中布尔变量的状态 .

我发现如果在mapStateToProps中我根据布尔触发器设置了另一个属性,那么组件就会被更新(重新渲染) .

function mapStateToProps(store, ownProps) {

  ...

  let triggerFetchSomethingOwnershipHash = 0;
  if (store.triggerFetchSomethingOwnership) {
    triggerFetchSomethingOwnershipHash = 100000 * Math.random();
  }

  return {
    ...
    triggerFetchSomethingOwnership,
    triggerFetchSomethingOwnershipHash 
  }
}

...
const ConnectedSomethingDetail = connect(mapStateToProps)(SomethingForm);
export default ConnectedSomethingDetail;

Why do I need to do this and how can I avoid it?

是的我已经检查了一些相关的SO问题,如react-redux-component-does-not-rerender-on-store-state-changereact-redux-update-item-in-array-doesnt-re-render