Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

componentWillReceiveProps called only once #2646

Closed
hkhere opened this issue Apr 1, 2019 · 5 comments
Closed

componentWillReceiveProps called only once #2646

hkhere opened this issue Apr 1, 2019 · 5 comments
Assignees

Comments

@hkhere
Copy link

hkhere commented Apr 1, 2019

问题描述
[问题描述:站在其它人的角度尽可能清晰地、简洁地把问题描述清楚]
componentWillReceiveProps will be called only once if dispatch is called multiple times on the same phase of the event loop (Assuming the component's props derived from redux store would be changed in the dispatch calls.).
复现步骤
[复现问题的步骤]

diff --git a/src/pages/index/index.js b/src/pages/index/index.js
index fa4f035..11ab0bf 100644
--- a/src/pages/index/index.js
+++ b/src/pages/index/index.js
@@ -12,6 +12,7 @@ import './index.css'
 }), (dispatch) => ({
   add () {
     dispatch(add())
+    dispatch(add())
   },
   dec () {
     dispatch(minus())

期望行为

componentWillReceiveProps (nextProps) {
    console.log(this.props, nextProps)
  }

There should be two console.log outputs..

报错信息

It has only one console.log output.

系统信息

Taro v1.2 及以上版本已添加 taro info 命令,方便大家查看系统及依赖信息,运行该命令后将结果贴下面即可。

Taro CLI 1.2.22 environment info:
System:
OS: Linux 4.15 Ubuntu 18.04.2 LTS (Bionic Beaver)
Shell: 4.4.19 - /bin/bash
Binaries:
Node: 10.15.1 - /usr/bin/node
Yarn: 1.13.0 - /usr/bin/yarn
npm: 6.4.1 - /usr/bin/npm
npmPackages:
@tarojs/async-await: 1.2.22 => 1.2.22
@tarojs/components: 1.2.22 => 1.2.22
@tarojs/plugin-babel: 1.2.22 => 1.2.22
@tarojs/plugin-csso: 1.2.22 => 1.2.22
@tarojs/plugin-uglifyjs: 1.2.22 => 1.2.22
@tarojs/redux: 1.2.22 => 1.2.22
@tarojs/redux-h5: 1.2.22 => 1.2.22
@tarojs/router: 1.2.22 => 1.2.22
@tarojs/taro: 1.2.22 => 1.2.22
@tarojs/taro-alipay: 1.2.22 => 1.2.22
@tarojs/taro-h5: 1.2.22 => 1.2.22
@tarojs/taro-swan: 1.2.22 => 1.2.22
@tarojs/taro-tt: 1.2.22 => 1.2.22
@tarojs/taro-weapp: 1.2.22 => 1.2.22
@tarojs/webpack-runner: 1.2.22 => 1.2.22
eslint-config-taro: 1.2.22 => 1.2.22
eslint-plugin-taro: 1.2.22 => 1.2.22
nervjs: ^1.3.9 => 1.3.13

补充信息
[可选]

// taro-redux  src/connect.js
 if (isChanged) {
      this.prevProps = prevProps
      this._unsafeCallUpdate = true
      this.setState({}, () => {
        delete this._unsafeCallUpdate
      })
    }
// taro-{weapp,...}  src/lifecycle.js
 if (component.__mounted && component._unsafeCallUpdate === true && component.componentWillReceiveProps) {
    component._disable = true
    component.componentWillReceiveProps(props)
    component._disable = false
  }

If dispatch is called multiple times on the same phase of the event loop. The component will be enqueued(enqueueRender) multiple times but the first callback then deletes this._unsafeCallUpdate, so component._unsafeCallUpdate === true will be false on next update in the current update queue.

@taro-bot
Copy link

taro-bot bot commented Apr 1, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@luckyadam luckyadam self-assigned this Apr 1, 2019
@luckyadam luckyadam added the Redux label Apr 1, 2019
@hkhere
Copy link
Author

hkhere commented Apr 1, 2019

Closing the issue because it's designed so.

@RunningCoderLee
Copy link

@luckyadam 请问这个问题官方有答复吗,在官方 react 和 redux 的使用中,每次 dispatch 都会触发 componentWillReceiveProps 的调用,但在 taro 中却没有这种预期行为,这个要怎么解决呢?

@lyboy2012
Copy link

监听props change 就没有好的方案吗 在componentDidUpdate 发现 prevProps 和this.props 是否相等也是随机的,就没有好的解决办法吗?

@hnsylitao
Copy link

这个是taro的设计如此还是留下来的问题。(如果设计如此,也应该在官方文档中指出把)
我在mobx 使用中 也碰到了同样的问题。
不过按照 @lyboy2012 应该可以通过componentDidUpdate 来达到。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants