Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: basic-features/data-fetching/client-side 更新 #290

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions docs/basic-features/data-fetching/client-side.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
description: 'Learn about client-side data fetching, and how to use SWR, a data fetching React hook library that handles caching, revalidation, focus tracking, refetching on interval and more.'
---

# Client-side data fetching

Client-side data fetching is useful when your page doesn't require SEO indexing, when you don't need to pre-render your data, or when the content of your pages needs to update frequently. Unlike the server-side rendering APIs, you can use client-side data fetching at the component level.

If done at the page level, the data is fetched at runtime, and the content of the page is updated as the data changes. When used at the component level, the data is fetched at the time of the component mount, and the content of the component is updated as the data changes.

It's important to note that using client-side data fetching can affect the performance of your application and the load speed of your pages. This is because the data fetching is done at the time of the component or pages mount, and the data is not cached.

## Client-side data fetching with useEffect

The following example shows how you can fetch data on the client side using the useEffect hook.

```jsx
function Profile() {
const [data, setData] = useState(null)
const [isLoading, setLoading] = useState(false)

useEffect(() => {
setLoading(true)
fetch('api/profile-data')
.then((res) => res.json())
.then((data) => {
setData(data)
setLoading(false)
})
}, [])

if (isLoading) return <p>Loading...</p>
if (!data) return <p>No profile data</p>

return (
<div>
<h1>{data.name}</h1>
<p>{data.bio}</p>
</div>
)
}
```

## Client-side data fetching with SWR

The team behind Next.js has created a React hook library for data fetching called [**SWR**](https://swr.vercel.app/). It is **highly recommended** if you are fetching data on the client-side. It handles caching, revalidation, focus tracking, refetching on intervals, and more.

Using the same example as above, we can now use SWR to fetch the profile data. SWR will automatically cache the data for us and will revalidate the data if it becomes stale.

For more information on using SWR, check out the [SWR docs](https://swr.vercel.app/docs/getting-started).

```jsx
import useSWR from 'swr'

const fetcher = (...args) => fetch(...args).then((res) => res.json())

function Profile() {
const { data, error } = useSWR('/api/profile-data', fetcher)

if (error) return <div>Failed to load</div>
if (!data) return <div>Loading...</div>

return (
<div>
<h1>{data.name}</h1>
<p>{data.bio}</p>
</div>
)
}
```

## Related

For more information on what to do next, we recommend the following sections:

<div class="card">
<a href="/docs/routing/introduction.md">
<b>Routing:</b>
<small>Learn more about routing in Next.js.</small>
</a>
</div>
9 changes: 7 additions & 2 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
},
{
"title": "データ取得",
"path": "/docs/basic-features/data-fetching.md"
"routes": [
{
"title": "Client side",
"path": "/docs/basic-features/data-fetching/client-side.md"
}
]
},
{
"title": "CSS のビルトインサポート",
Expand Down Expand Up @@ -427,4 +432,4 @@
]
}
]
}
}