Next.jsNext.js·

Fetching data from App Router client component in Nextjs

Publié à 2024-07-20 18:10:43Vu 194 fois
Article professionnel
Réimpression Veuillez indiquer la source

App Router is a new feature of Next.js since Next.js 13. It works within the app directory of the application. However, compared to Page Router, Nextjs documentation is lacking sufficient illustrations. For example, in server component of App Router, you can directly call fetch to fetch data from api. But when it comes to fetching data in client component, the documentation just claiming that you need to call a Route Handler from a client component, and the other doc does not actually contain any such example.

There are two ways to fetch data on a client component in App Router. The most easiet one is to use a third-party library such as SWR. However, in this article, we will give you a short tutorial on how to fetch data on a client component using Route Handler.

First, create a new folder api in the app directory. Then depending on your requirement, you can also create a folder login in api. Next, to complete creating a Route Handler, create a new file route.js|ts in the login directory. Then your folder structure will like the following:

\app
   \api
      \login
          route.js|ts

Now, add the following to your route.js|ts:

export async function GET() {
    const res = await fetch('https://www.example.com/api/...', {
      headers: {
        'Content-Type': 'application/json',
      },
    })
    const data = await res.json()
   
    return Response.json({ data })
}

Finally, call the Route Handler you defined in a client component as follows:

"use client";

export default function Page({ children }: { children: any }) {
  fetch('/api/login')
  .then((res)=>
    res.json()
  )
  .then((data)=>{
    console.log(data)
  })
}

You can see that creating a Route Handler is indeed creating a new route and URL in your application. Then to use the Route Handler to fetch data, you just need to call the URL you defined. In other words, calling a Route Handler means calling its URL.

Section des commentaires

Pas encore de commentaire, ajoutez le premier.

弦圈热门内容

pyttsx3运行错误

接上文Python实现语音朗读,运行示例代码时import pyttsx3 engine = pyttsx3.init() engine.say('开车不规范,亲人两行泪,I love China') engine.runAndWait()弹出以下错误:经过检查,pywin32等库都已经安装好了。尝试使用win32com库替代pyttsx3,结果仍然报错,报错内容为win32 api。之后又尝试了几种办法,仍然都是跟win32有关的报错。因为之前pip安装总是SSL报错,刚开始以为是SSL报错导致安装出错。但是修复SSL报错问题后(见Python pip安装SSL证书错误),该问题仍然没解决。最后经过了解,可能是pywin32版本过高所导致。一般需要将pywin32版本控制在305以下,可以使用225或者226这样的低版本。于是使用pip下载对应版本pip install pypiwin32 pip install pywin32 == 225然而,下载时发现已经没有225版本可以下载。因此另寻办法。最终,发现是pywin32安装的版本有问题,导致包虽然有了,但是却无法识别,导致出现N ...

大学毕业转行后的一点想法

最近成功把以前写的PDF格式的数学文章,几乎完美复刻到HTML网页上面,文章中的数学公式使用JS插件Mathjax渲染。之后会陆续更新到网站上,希望以后能让更多人无需下载就能看到,这也算给大学四年一个结尾。链接如下👇👇👇Note on arithmetic algebraic geometry, An introduction to different branches of mathematics, Note on perfectoid spaces, 代数几何简介​然后我目前只会把我以前留下的notes、introduction之类的弄成HTML这样网页的形式。至于我写的论文存arXiv上面就好了,谷歌搜也能搜到我的论文。目前来看,距离我论文完成也过去一年半了,并没有太多人对于推广perfect这一概念感兴趣。但值得一提的是,目前来看,我的工作更加受到老外的欣赏和认可,没有一个中国的Phd给我写过信,说看过我的文章。虽然关于perfect这一系列的工作没有全部完成,还可以继续深入耕耘,说不定还能多产出几篇论文吧,算下来我本科完成了4篇论文,有5篇未完成,总页数超过100页。但这一切 ...