1

开发环境
· next.js
· typescript
· swr

它使用 swr 与 swr 通信。
我只想在查询的值更改时运行它。
但也在初始绘图期间执行。
我能做些什么来防止它在初始绘图期间运行?

export const useUserQuery = (query: string) => {
  const fetcher = (url) => {
    Axios.get(url).then((res) => {
      return res;
    });
    const path = `users/?query=${query}`;
    const { data, error } = useSWR<IUser>(path);
    return {
      data: data,
      loading: !error && !data,
      Error: error,
    };
  };
};
4

2 回答 2

0

更新:

使用initalData并禁用revalidateOnMount它应该可以按您的预期工作。

在文档中查看更多信息:https ://github.com/vercel/swr

于 2020-10-26T00:09:02.187 回答
-1

It seems that you what to refeatch on variable change. If you try this, it won't work, because even if token changes, SWR will still use the same key and return the wrong data.

useSWR('/api/user', url => fetchWithToken(url, token))

Instead, you can use an array as the key parameter, which contains multiple arguments of fetcher:

const { data: user } = useSWR(['/api/user', token], fetchWithToken)

See more in docs

于 2020-12-31T13:07:36.517 回答