Taro: 使用taro-with-weapp withWeapp HOC组件 调用 componentWillMount 报 Cannot read property 'params' of undefined

Created on 14 Jul 2020  ·  7Comments  ·  Source: NervJS/taro


相关平台

微信小程序

小程序基础库: 2.11.3
使用框架: React

复现步骤

@withWeapp("Page")
class _C extends React.Component {}

只要使用了这个装饰器一定会报错,基于React框架取到的this实例没有$router属性。

具体代码:https://github.com/NervJS/taro/blob/next/packages/taro-with-weapp/src/index.ts#L226

参考文档:https://taro-docs.jd.com/taro/docs/migration#%E8%B7%AF%E7%94%B1

期望结果

正常运行不报错

实际结果

Cannot read property 'params' of undefined

环境信息

Taro CLI 3.0.2 environment info:
    System:
      OS: macOS 10.15.5
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 12.4.0 - /usr/local/bin/node
      Yarn: 1.22.4 - ~/.yarn/bin/yarn
      npm: 6.14.5 - /usr/local/bin/npm
    npmPackages:
      @tarojs/components: 3.0.2 => 3.0.2 
      @tarojs/mini-runner: 3.0.2 => 3.0.2 
      @tarojs/react: 3.0.2 => 3.0.2 
      @tarojs/runtime: 3.0.2 => 3.0.2 
      @tarojs/taro: ^3.0.2 => 3.0.2 
      @tarojs/webpack-runner: 3.0.2 => 3.0.2 
      babel-preset-taro: 3.0.2 => 3.0.2 
      eslint-config-taro: 3.0.2 => 3.0.2 
      react: ^16.13.1 => 16.13.1 
    npmGlobalPackages:
      typescript: 3.9.5

A-taroize V-3

Most helpful comment

BaseComponent.prototype.componentWillMount = function componentWillMount() {
   ...
   this.executeLifeCycles(this.willMounts, this.$router.params || {});
 };

this 上 不存在 $router 对象,报错 Cannot read property 'params' of undefined

另外,使用 taro convert (taro v3.0.5) 将微信小程序转换为 ReactVue 时,会出现同样的问题
当转换为 React 时,会出现相同的报错 Cannot read property 'params' of undefined
当转换为 Vue 时,会出现 Cannot set property 'render' of undefined 的错误

这个致命错误,会导致页面空白,无法渲染

All 7 comments

this.executeLifeCycles(this.willMounts, this.$router ? this.$router.params : {}); 魔改后不报错,取到的this.state为空

遇到同样的问题,求解决

这个 bug 很棘手吗,升级遇到的问题迫切需要解决

BaseComponent.prototype.componentWillMount = function componentWillMount() {
   ...
   this.executeLifeCycles(this.willMounts, this.$router.params || {});
 };

this 上 不存在 $router 对象,报错 Cannot read property 'params' of undefined

另外,使用 taro convert (taro v3.0.5) 将微信小程序转换为 ReactVue 时,会出现同样的问题
当转换为 React 时,会出现相同的报错 Cannot read property 'params' of undefined
当转换为 Vue 时,会出现 Cannot set property 'render' of undefined 的错误

这个致命错误,会导致页面空白,无法渲染

同求怎么解决,1.x升级到2.x以后很多页面this.state是undefined,要都去掉@withweapp的话改动太大,这几天分享到朋友圈加不上头发都白了

@SnowHot @luckyadam 白了还能染回来,秃了就麻烦了。withweapp 挂了,业务代码用到太多,从 2 到 3 的升级基本无望。感觉官方人手不够,bug太多了,这个优先级应该提高啊。

已修复

Was this page helpful?
0 / 5 - 0 ratings