Next.js useSWR在fetcher函数中返回正确的数据,但不在main函数中返回正确的数据

Next.js useSWR在fetcher函数中返回正确的数据,但不在main函数中返回正确的数据,next.js,Next.js,我在我的NextJS项目中使用useSWR,但它返回未定义时遇到问题 hooks.jsx import useSWR from 'swr'; import { useEffect } from 'react'; import { useRouter } from 'next/router'; const fetcher = (url) => { fetch(url) .then((r) => r.json()) .then((data) =&

我在我的NextJS项目中使用useSWR,但它返回未定义时遇到问题

hooks.jsx

import useSWR from 'swr';
import { useEffect } from 'react';
import { useRouter } from 'next/router';

const fetcher = (url) => {
    fetch(url)
        .then((r) => r.json())
        .then((data) => {
            return { user: data?.user || null };
        });
};

function useUser({ redirectTo, redirectIfFound } = {} ) {
    const router = useRouter();

    const { data, error } = useSWR('/api/users/fetch', fetcher);
    const user = data?.user;
    const finished = Boolean(data);
    const hasUser = Boolean(user);

    console.log(user);

    useEffect(() => {
        if (!redirectTo || !finished) return;

        if (
            // for register / signin route
            (redirectTo && redirectIfFound && hasUser)
        ) {
            router.push(redirectTo);
        }
    }, [redirectTo, redirectIfFound, finished, hasUser]);

    return error ? null : user;
}

export default useUser;
当我的控制台在fetcher函数中记录“data”时,它会按预期返回数据。但是,在主useUser函数中,“data”返回未定义的值。我做错了什么


提前感谢

您的回执程序中的回执不会被返回