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

在页面的 onUnload 生命周期调用 Taro.getCurrentInstance 获取不到 page 和 router #9189

Closed
lblblong opened this issue Apr 25, 2021 · 2 comments
Labels
F-react Framework - React T-h5 Target - 编译到 H5 V-3 Version - 3.x walk-around 绕过问题

Comments

@lblblong
Copy link
Contributor

相关平台

H5

复现仓库

https://github.com/lblblong/taro-h5-onunload-issue
浏览器版本: Edge 90.0.818.46
使用框架: React

复现步骤

进入主页后进入第二个页面,在第二个页面返回时,onUnload 生命周期中调用 Taro.getCurrentInstance 获取不到 page 和 router

编译到 小程序环境就可以获取到,h5不行

期望结果

h5 Page.onUnload 通过 Taro.getCurrentInstance 获取到 page 和 router

实际结果

获取不到

环境信息

 Taro v3.2.1


  Taro CLI 3.2.1 environment info:
    System:
      OS: Windows 10
    Binaries:
      Node: 14.15.4 - E:\app\nodejs\.nvm\v14.15.4\node.EXE
      Yarn: 1.22.10 - E:\app\nodejs\.nvm\v14.15.4\yarn.CMD
      npm: 6.14.10 - E:\app\nodejs\.nvm\v14.15.4\npm.CMD 
@taro-bot2 taro-bot2 bot added F-react Framework - React T-h5 Target - 编译到 H5 V-3 Version - 3.x labels Apr 25, 2021
@Chen-jj
Copy link
Contributor

Chen-jj commented Apr 25, 2021

@lblblong 闭包起来用吧:

import React, { Component } from "react";
import { View, Text, Button } from "@tarojs/components";
import "./index.css";
import Taro from "@tarojs/taro";

export default class Index extends Component {

  componentWillMount() {
    // debugger
    const instance = Taro.getCurrentInstance()
    const pageConfig = instance.page

    const originOnUnload = pageConfig.onUnload;
    pageConfig.onUnload = function () {
      // debugger
      console.log("页面卸载", pageConfig);
      originOnUnload.apply(this);
    };
  }

  componentDidMount() {}

  componentWillUnmount() {}

  componentDidShow() {}

  componentDidHide() {}

  render() {
    return (
      <View className="index">
        <Text>Hello world!</Text>
        <Button
          onClick={() => {
            Taro.navigateBack();
          }}
        >
          返回
        </Button>
      </View>
    );
  }
}

微信小程序 onUnLoad 触发前不会触发 onHide,H5 则规定 onUnLoad 触发前会触发 onHide。而 onHide 时内部会把 instance.page 置为 null:

onHide () {
Current.page = null
Current.router = null
const path = getPath(id, this.options)
safeExecute(path, 'onHide')
eventCenter.trigger(getOnHideEventKey(id))
},

@Chen-jj Chen-jj closed this as completed Apr 25, 2021
@Chen-jj Chen-jj added the walk-around 绕过问题 label Apr 25, 2021
@lblblong
Copy link
Contributor Author

有一些外部代码,比如第三方库调用 Taro.getCurrentInstance 时怎么办呢,为什么不让 h5 和小程序行为一致

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-react Framework - React T-h5 Target - 编译到 H5 V-3 Version - 3.x walk-around 绕过问题
Projects
Archived in project
Development

No branches or pull requests

2 participants