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

Add hooks before and after request #42

Closed
John60676 opened this issue May 6, 2021 · 5 comments
Closed

Add hooks before and after request #42

John60676 opened this issue May 6, 2021 · 5 comments
Labels

Comments

@John60676
Copy link
Member

Usage

const request = useRequest(server, {
  onBefore(params) {},
  onAfter(params) {},
})

API

Options

Property Description Type Default
onBefore Hook before request (params: P) => void -
onAfter Hook after request (params: P) => void -
@keuby
Copy link

keuby commented May 10, 2021

@John60676 onBefore 钩子在request 之前就直接执行了,但是 loading 这个时候可能还没有触发,如果直接在这个时候去执行显示loading的话,会出现 loading 闪现的情况

  const delayLoading = () => {
    let timerId: number;

    if (loadingDelay) {
      timerId = setTimeout(setState, loadingDelay, {
        loading: true,
      });
    }

    return () => timerId && clearTimeout(timerId);
  };

不知道能不能再这里有什么东西可以检测到 loading 的变化

@John60676
Copy link
Member Author

@keuby 有了 onBeforeonAfter 后,其实可以自己封装 loading

@keuby
Copy link

keuby commented May 10, 2021

有了 onBefore 和 onAfter 后,其实可以自己封装 loading

嗯,这个没啥问题,不过 getGlobalOptions 这个方法可能得暴露出来才行。用户拿不到 getGlobalOptions 这个方法,这个 loadingDelay 的值除了 setGlobalOptions 那边写了一遍以外,这边可能还得再写一遍,这个有点儿不太合理。

@John60676
Copy link
Member Author

但是这个 config 目前还没有想到特别好的方案能暴露出去,等我后续再研究一下。如果你有好的方案,欢迎来个PR😁

目前希望的API形式是

const request = useRequest(server, {
  onBefore(params, config) {},
  onAfter(params, config) {},
})

@John60676
Copy link
Member Author

v1.2.0 已发布

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

No branches or pull requests

2 participants