Taro: componentWillReceiveProps 触发异常

Created on 18 Dec 2018  ·  9Comments  ·  Source: NervJS/taro

问题描述
如果在componentWillReceiveProps 中 dispatch redux 的action,this.props 和nextProps 的值会异常

复现步骤

  1. git clone https://github.com/KevinGong2013/redux-will-receive-props-demo
  2. npm run dev:weapp
  3. 点击 +
  4. ide 大概会卡死,componentWillReceiveProps 无限调用

期望行为
componentWillReceiveProps 调用过一次 this.props 应该更新为 nextProps的值, 然而没有更新

系统信息

  • 操作系统: [Mac OS X 10.14.1 (18B75)]
  • Taro 版本 [v.1.2.0]
  • Node.js 版本 [v11.2.0]
  • 报错平台 [alipay, weapp]
question

All 9 comments

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

这个有没有修复的排期呀, 或者推荐的workaround办法呀~

遇到同样的问题,在 componentWillReceiveProps 里 执行 action ,因为状态没有更新执行了几百次

明天会进行修复

大佬这个问题有计划了不 😢

image
这样写在小程序中肯定会导致不断执行

因为 this.props.dispatchDoNothing() 这个 action 什么都不做,但是会触发 componentWillReceiveProps ,这个时候 this.props 还没有更新

不建议这么写,你可以再增加其他判断条件

Hello~

您的问题楼上已经有了确切的回答,如果没有更多的问题这个 issue 将在 15 天后被自动关闭。

如果您在这 15 天中更新更多信息自动关闭的流程会自动取消,如有其他问题也可以发起新的 Issue。

Good luck and happy coding~

那好吧 这里提供一个workaround方法

screen shot 2018-12-26 at 16 47 32

如果考虑只执行一次action的话,也可以这样:

  componentWillReceiveProps(nextProps: IProps) {
    console.log(this.props, nextProps)
    if (!this.flag) {
      this.flag = true;
      this.props.dispatchDoNothing()
    }
  }

但是请问官方有更好的解决方案吗?为啥this.props没更新?

Was this page helpful?
0 / 5 - 0 ratings