🏖️
Chisfis - Online Booking NextJs template
DemoBuy now
  • Getting Started
  • Folder structure and organization
  • Dependencies
  • Installation
  • Custom color, fonts
  • CSS
  • Component Structure
  • How to fetch data
  • How to deploy
  • How to add metadata
  • Resources and assets
  • Support
  • Change log
    • Change log
  • How to?
    • How to Get Google Maps API Key for Free (in 5 easy steps)
    • Converting a Next.js project from TypeScript to plain JavaScript
Powered by GitBook
On this page
  • Server Components
  • Client Components

Was this helpful?

How to fetch data

PreviousComponent StructureNextHow to deploy

Last updated 3 days ago

Was this helpful?

This page will walk you through how you can fetch data in and . As well as how to content that depends on data.

Here is a tutorial on how to fetch data. This entire documentation is referenced from Nextjs, so for a better overview, please check this tutorial from Nextjs -

You can fetch data in Server Components using:

  1. The

  2. An

To fetch data with the fetch API, turn your component into an asynchronous function, and await the fetch call.

For example:

app/blog/page.tsx

export default async function Page() {
  const data = await fetch('https://api.vercel.app/blog')
  const posts = await data.json()
  return (
    <ul>
      {posts.map((post) => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  )
}

Since Server Components are rendered on the server, you can safely make database queries using an ORM or database client. Turn your component into an asynchronous function, and await the call:

app/blog/page.tsx

import { db, posts } from '@/lib/db'
 
export default async function Page() {
  const allPosts = await db.select().from(posts)
  return (
    <ul>
      {allPosts.map((post) => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  )
}

There are two ways to fetch data in Client Components, using:

app/blog/page.tsx

import Posts from '@/app/ui/posts
import { Suspense } from 'react'
 
export default function Page() {
  // Don't await the data fetching function
  const posts = getPosts()
 
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <Posts posts={posts} />
    </Suspense>
  )
}

Then, in your Client Component, use the use hook to read the promise:

app/ui/posts.tsx

'use client'
import { use } from 'react'
 
export default function Posts({
  posts,
}: {
  posts: Promise<{ id: string; title: string }[]>
}) {
  const allPosts = use(posts)
 
  return (
    <ul>
      {allPosts.map((post) => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  )
}

app/blog/page.tsx

React's

A community library like or

You can use React's to data from the server to client. Start by fetching data in your Server component, and pass the promise to your Client Component as prop:

In the example above, you need to wrap the <Posts /> component in a . This means the fallback will be shown while the promise is being resolved. Learn more about .

You can use a community library like or to fetch data in Client Components. These libraries have their own semantics for caching, streaming, and other features. For example, with SWR:

Server Components
Client Components
stream
https://nextjs.org/docs/app/getting-started/fetching-data
Server Components
fetch API
ORM or database
With the fetch API
With an ORM or database
Client Components
use hook
SWR
React Query
With the use hook
use hook
stream
<Suspense> boundary
streaming
Community libraries
SWR
React Query