·

Fetching data from App Router client component in Nextjs

Published at 2024-07-20 18:10:43Viewed 270 times
Professional article
Please reprint with source link

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.

0 人喜欢

Comments

There is no comment, let's add the first one.

弦圈热门内容

Django change an existing field to foreign key

I have a Django model that used to look like this:class Car(models.Model): manufacturer_id = models.IntegerField()There is another model called Manufacturer that the id field refers to. However, I realized that it would be useful to use Django's built-in foreign key functionality, so I changed the model to this:class Car(models.Model): manufacturer = models.ForeignKey(Manufacturer)This change appears to work fine immediately, queries work without errors, but when I try to run migrations, Django outputs the following:- Remove field manufacturer_id from car - Add field manufacturer to carDoing this migration would clear all the existing relationships in the database, so I don't want to do that. I don't really want any migrations at all, since queries like Car.objects.get(manufacturer__name="Toyota") work fine. I would like a proper database foreign key constraint, but it's not a high priority.So my question is this: Is there a way to make a migration or something else that allows me to convert an existing field to a foreign key? I cannot use --fake since I need to reliably work across dev, prod, and my coworkers' computers.内容来源于 Stack Overflow, 遵循 CCBY-SA 4.0 许可协议进行翻译与使用。原文链接:Django change an existing field to foreign key

Get connected with us on social networks! Twitter

©2024 Guangzhou Sinephony Technology Co., Ltd All Rights Reserved