跳转到内容
彼岸论坛
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Node.js] 前端小白,咨询下 nextjs, react 项目 useQuery 相关问题,多谢各位大佬


小天管理

已推荐帖子

下面代码,为啥 Hello1 正常,Hello2 不正常? console.log 输出分别为: Hello1: success {Hello: 'World'} null false Hello2: undefined undefined undefined undefined

src/api/test3.tsx

'use client';

import { useQuery } from "@tanstack/react-query"
import axios from "axios"


function TestApi3() {

    return {
        Hello1,
        Hello2,
    }

    function Hello1() {
        return useQuery({
            queryKey: ["hello2"],
            queryFn: async () => {
                const { data } = await axios.get("http://localhost:8000/api/hello", )
                return data;
            }
        })
    }

    function Hello2() {
        return useQuery({
            queryKey: ['hello3'],
            queryFn: async () => {
                const data = (await fetch('http://localhost:8000/api/hello')).json()
                return data
            },
        })
    }
}

export { TestApi3 }

src/app/debug/page.tsx

'use client';

import { TestApi3 } from "@/api/test3"

export default function Home() {
    const { status, data, error, isFetching } = TestApi3().Hello1();
    console.log('Hello1:', status, data, error, isFetching)

    const { status2, data2, error2, isFetching2 } = TestApi3().Hello2();
    console.log('Hello2:', status2, data2, error2, isFetching2)


    return (
        <main className="">
            <div>
                <h1>Hello World</h1>
            </div>

        </main>
    );
}

意见的链接
分享到其他网站

加入讨论

您现在可以发表并稍后注册. 如果您是会员,请现在登录来参与讨论.

游客
回复主题...

×   粘贴为富文本.   粘贴为纯文本来代替

  只允许使用75个表情符号.

×   您的链接已自动嵌入.   显示为链接来代替

×   您之前的内容已恢复.   清除编辑器

×   您无法直接粘贴图片.要从网址上传或插入图片.

  • 游客注册

    游客注册

  • 会员

    没有会员可显示

  • 最新的状态更新

    没有最新的状态更新
  • 最近查看

    • 没有会员查看此页面.
×
×
  • 创建新的...