简体   繁体   English

当导航参数更新时使用 useSWR 进行 api 调用,并将其值存储在带有 contextAPI 的自定义挂钩上

[英]Make api call with useSWR when navigation params updates and store his value on custom hook with contextAPI

I want to make an API call with useSWR when navigation(route)'s params updates and store his value on a custom hook with contextAPI.我想在导航(路由)的参数更新时使用 useSWR 进行 API 调用,并将其值存储在带有 contextAPI 的自定义挂钩上。

I have this component UsersPage that renders a list of users, it needs to have pagination, and when I click on some user, share his link based on his id and query requested (page, userId, nationality=nat, gender, etc).我有这个组件UsersPage呈现用户列表,它需要分页,当我点击某个用户时,根据他的 ID 和请求的查询(页面、用户 ID、国籍 = nat、性别等)分享他的链接。

I'm considering using useSWR for a better User Experience.我正在考虑使用 useSWR 来获得更好的用户体验。

I tried我试过

import { useUsers } from "../../hooks/useUsers";
import { useFetch } from "../../hooks/useFetch";

.
.
.

const { pageNumber, userId } = useParams();

const {
    selectedGender,
    nationality,
    results,
    showDetails,
    page,
    isAscending,
    toggleIsAscending,
    selectedUser,
    formattedName,
    formattedBirth,
    getQuery,
  } = useUsers();

useEffect(() => {

    const users = useFetch(getQuery());
//then update users state on useUsers Context's hook and render data on Context's child nodes
  }, [pageNumber]);

on custom hook useUsers the function getQuery is在自定义挂钩 useUsers 上,function getQuery 是

  const getQuery = () => {
    const query = `&gender=${selectedGender}&nat=${nationality}&page=${page}&results=${results}`;

    return query;
  };

On useFetch hook I have:在 useFetch 挂钩上,我有:

import useSWR from "swr";

export function useFetch(query) {
  const API_URL = process.env.REACT_APP_API_URL;

  const fetcher = async (query) => {
    const FormedURL = `${API_URL}${query}`;

    const response = await fetch(FormedURL);
    const data = await response.json();

    return data.results;
  };
  const { data, error } = useSWR(query, fetcher);

  return { users: data, error };
}

I received: 'React Hook "useFetch" cannot be called inside a callback.我收到:'无法在回调中调用 React Hook“useFetch”。 React Hooks must be called in a React function component or a custom React Hook function.'必须在 React function 组件或自定义 React Hook function 中调用 React Hooks。

Have someone done this before and share your experience/advice?以前有人这样做过并分享您的经验/建议吗?

I would try something like this:我会尝试这样的事情:

const { pageNumber, userId } = useParams();

const {
    selectedGender,
    nationality,
    results,
    showDetails,
    page,
    isAscending,
    toggleIsAscending,
    selectedUser,
    formattedName,
    formattedBirth,
    getQuery,
  } = useUsers();

const { get, response } = useFetch(getQuery())

const fetchUsers = async () {
    const users = await get()
    if (response.ok){
        // sets users...
    }
}

useEffect(() => {
    fetchUsers()
}, [pageNumber]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我的响应需要在下一个依赖 API 调用之前执行一些逻辑操作时,我如何使用 useSWR 挂钩 - How can i use the useSWR hook when my response need to do some logical operations before next dependant API call 自定义挂钩-根据API调用设置值 - Custom hook - setting value based on API call 如何遍历 URL 列表并使用 useSWR 挂钩调用服务器 - How can I iterate over the list of URLs and make a call to the server using useSWR hook 如何根据另一个请求的响应向 API 发出请求? 不能在回调中调用 React Hook “useSwr” - How can I make a request to an API based on the response from another request? React Hook “useSwr” cannot be called inside a callback 在 nextjs 中获取动态查询参数后如何调用 useSWR? - How to call useSWR after getting dynamic query params in nextjs? 调用自定义钩子时无效的钩子调用 - Invalid Hook Call when calling a custom hook 如何使用给定的初始值进行 useSWR 突变? - How to make useSWR mutations with a given initial value? 我应该在这个包装 useSWR 的自定义钩子中测试什么? - What should i test in this custom hook that is wrapping useSWR? useSWR Hook 返回 undefined - useSWR Hook returns undefined 在使用立即需要该状态的useState挂钩进行API调用时,如何等待setState调用结束? - How to await a setState call finishing when using useState hook that immediately needs that state to make an API call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM