How to fetch data
Last updated
Was this helpful?
Last updated
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:
The
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
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
There are two ways to fetch data in Client Components, using:
app/blog/page.tsx
Then, in your Client Component, use the use
hook to read the promise:
app/ui/posts.tsx
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: