umijs / umi

A framework in react community ✨
https://umijs.org
MIT License
15.08k stars 2.64k forks source link

[Bug] history.block异常导致页面路由无法正常跳转 #10911

Closed NieShiyi closed 1 year ago

NieShiyi commented 1 year ago

What happens?

history.block的回调函数不管有没有返回值或是返回任何值,都会阻止路由跳转。按理说teturn true应该是允许正常跳转的,但是现在还是被阻止了,都无法正常跳转页面。

Mini Showcase Repository(REQUIRED)

https://github.com/NieShiyi/umi4-antdpro-style/tree/umi-history-block

Please provide a minimal reproduction then upload to your GitHub. 请提供 最小重现,并上传到你的 GitHub 仓库

How To Reproduce

Steps to reproduce the behavior: 1. 2. 点击左侧的菜单,发现跳转被阻止了 Expected behavior 1. 2. 期望history.block的回调函数里返回true时,可以正常跳转

Context

1.随便在一个页面函数中写如下代码

  useEffect(() => {
    const unblock = history.block(({ location }) => {
      console.log('地址被拦截', location)
      // return false
      // return "hello"
      return true;
    });
    return () => {
      unblock?.();
    };
  }, []);

2.随意点击菜单跳转,发现无法跳转

fz6m commented 1 year ago

这是 history.block(() => {}) 中回调函数的类型,返回值是 void ,没有你说的功能:

/**
 * A function that receives transitions when navigation is blocked.
 */
export interface Blocker {
    (tx: Transition): void;
}
NieShiyi commented 1 year ago

参照这个文档 https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md

useEffect(() => {
    const unblock = history.block(({ location , retry}) => {
      // 想要路由不被拦截,先取消监听,在调用retry方法
      // unblock();
      // retry();

      // 时长这样使用
      if(!window.confirm("是否离开该页面?")){
        console.log('no')
      }else{
        unblock();
        retry();
      }
    });
    return () => {
      unblock?.();
    };
 }, []);