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

componentWillMount里面执行redirectTo, componentDidShow和componentDidMount仍然被执行 #4312

Closed
dpyzo0o opened this issue Aug 27, 2019 · 6 comments

Comments

@dpyzo0o
Copy link

dpyzo0o commented Aug 27, 2019

问题描述
componentWillMount里面执行redirectTo, componentDidShow和componentDidMount仍然被执行

复现步骤

代码截图, 左边为 pages/index, 右边为 pages/login

Screen Shot 2019-08-27 at 19 25 49

执行顺序

Screen Shot 2019-08-27 at 19 23 23


**期望行为**
redirectTo之后不应该再执行生命周期函数

**报错信息**

[这里请贴上你的**完整**报错截图或文字]

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

  Taro CLI 1.3.10 environment info:
    System:
      OS: macOS High Sierra 10.13.6
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 10.15.3 - ~/.nvm/versions/node/v10.15.3/bin/node
      Yarn: 1.16.0 - ~/.nvm/versions/node/v10.15.3/bin/yarn
      npm: 6.4.1 - ~/.nvm/versions/node/v10.15.3/bin/npm
    npmPackages:
      @tarojs/components: 1.3.15 => 1.3.15 
      @tarojs/plugin-babel: 1.3.15 => 1.3.15 
      @tarojs/plugin-csso: 1.3.15 => 1.3.15 
      @tarojs/plugin-uglifyjs: 1.3.15 => 1.3.15 
      @tarojs/router: 1.3.15 => 1.3.15 
      @tarojs/taro: 1.3.15 => 1.3.15 
      @tarojs/taro-alipay: 1.3.15 => 1.3.15 
      @tarojs/taro-h5: 1.3.15 => 1.3.15 
      @tarojs/taro-qq: 1.3.15 => 1.3.15 
      @tarojs/taro-quickapp: 1.3.15 => 1.3.15 
      @tarojs/taro-swan: 1.3.15 => 1.3.15 
      @tarojs/taro-tt: 1.3.15 => 1.3.15 
      @tarojs/taro-weapp: 1.3.15 => 1.3.15 
      @tarojs/webpack-runner: 1.3.15 => 1.3.15 
      eslint-config-taro: 1.3.15 => 1.3.15 
      eslint-plugin-taro: 1.3.15 => 1.3.15 
      nerv-devtools: ^1.4.4 => 1.4.4 
      nervjs: ^1.4.4 => 1.4.4 
      stylelint-config-taro-rn: 1.3.15 => 1.3.15 
      stylelint-taro-rn: 1.3.15 => 1.3.15 
    npmGlobalPackages:
      typescript: 3.4.5

**补充信息**
[可选]
[根据你的调查研究,出现这个问题的原因可能在哪里?]
@Chen-jj
Copy link
Contributor

Chen-jj commented Aug 28, 2019

@dpyzo0o 你可以写份原生试试在 onLoad 跳转还会不会触发 onReady、onShow

@dpyzo0o
Copy link
Author

dpyzo0o commented Aug 28, 2019

@Chen-jj 试了下,原生的也是会触发 onReady 和 onShow
Screen Shot 2019-08-28 at 10 54 46

我想请问一下所以一般这种情况是如何处理的呢?我是需要在 componentWillMount 里面检查是否登录过,未登陆过就跳转登录页,但是在 componentDidMount 和 componentDidShow 里面有需要登录权限的网络请求,这种情况就会报错。

@Chen-jj
Copy link
Contributor

Chen-jj commented Aug 28, 2019

@dpyzo0o componentDidMount 和 componentDidShow 里没有登录权限就不请求直接 return 不就行咯

@dpyzo0o
Copy link
Author

dpyzo0o commented Aug 28, 2019

@Chen-jj 恩恩,我目前也是这么处理的。但是感觉每个需要登录权限的页面都要在三个生命周期里面检查一下,很繁琐。。

@Chen-jj
Copy link
Contributor

Chen-jj commented Aug 28, 2019

@dpyzo0o 试试 hooks 吧,抽一个 useEffect

@dpyzo0o
Copy link
Author

dpyzo0o commented Aug 28, 2019

@Chen-jj 好的,感谢解答~

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

2 participants