0Pricing
Next.js 15 Fullstack (App Router + Server Actions) · Ders

Sunucu Durumunu Yönetme

SWR veya React Query dahil olmak üzere sunucudan alınan verileri yönetme ve bunları kullanıcı arayüzünüzle eşzamanlı tutma stratejilerini keşfedin.

Sunucu Durumunu Yönetme, CoddyKit'te ücretsiz bir Next.js 15 Fullstack (App Router + Server Actions) dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Next.js 15 Fullstack (App Router + Server Actions) öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Next.js 15 Fullstack (App Router + Server Actions) kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

What is Server State?

When building applications, you deal with different kinds of data. Server state refers to data that lives on a remote server, like a list of products, user profiles, or blog posts.

It's distinct from client state, which is data managed purely within your application, such as whether a modal is open or a theme is dark/light.

Challenges of Server State

Managing server state can be tricky because:

  • It's Asynchronous: Data isn't instantly available; it takes time to fetch.
  • It Can Be Stale: The data on the server might change after you've fetched it.
  • Caching is Hard: How do you store fetched data efficiently and know when to refetch?
  • Error Handling: Network requests can fail, requiring robust error management.
  • Loading States: Users need feedback while data is being fetched.

Introducing SWR & React Query

Libraries like SWR (Stale-While-Revalidate) and React Query (now TanStack Query) are designed specifically to tackle these server state challenges in React applications.

They provide powerful hooks that simplify data fetching, caching, revalidation, and error handling, making your UI more robust and responsive.

Simple Asynchronous Fetch

Before using a library like SWR, fetching data asynchronously often involves manual handling of loading, success, and error states. Here's a basic JavaScript example:

async function fetchData() {
  console.log("Fetching data...");
  try {
    const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const data = await response.json();
    console.log("Data fetched: ", data.title);
  } catch (error) {
    console.error("Fetch error: ", error.message);
  }
}

fetchData();

The useSWR Hook

SWR wraps this fetching logic into a convenient React Hook called useSWR. It automatically provides the data, error, and a loading state (implicitly via data being undefined initially).

You provide a unique key (often the API endpoint) and a fetcher function that makes the actual data request.

import useSWR from 'swr';

const fetcher = url => fetch(url).then(res => res.json());

function UserProfile() {
  // The key is '/api/user', the fetcher is our function
  const { data, error } = useSWR('/api/user', fetcher);

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

  return <div>Hello, {data.name}!</div>;
}
// In a Next.js app, UserProfile would be rendered
// inside a page component.

Smart Caching & Deduping

One of SWR's core features is its intelligent caching. When multiple components request data with the same key, SWR automatically deduplicates these requests, fetching the data only once.

It then shares this cached data across all subscribers, preventing unnecessary network calls and improving performance. This is crucial for large applications.

Stale-While-Revalidate Strategy

SWR's name comes from its Stale-While-Revalidate caching strategy. This means:

  • It immediately returns the cached (stale) data to the UI.
  • It then sends a request to revalidate (fetch new) data in the background.
  • Once new data arrives, it updates the UI.

This provides an instant user experience while ensuring data freshness.

Automatic Revalidation

SWR and React Query automatically revalidate data in several common scenarios, ensuring your UI always reflects the latest server state:

  • On Focus: When the browser tab or window regains focus.
  • On Reconnect: When the network connection is restored.
  • On Interval: You can configure periodic revalidation for frequently changing data.

This significantly reduces the need for manual data refreshing.

Mutating Data & UI Updates

When you perform actions that change server data (e.g., submitting a form to create a new post), you need to update your UI. SWR provides a mutate function to help with this.

You can use mutate to manually revalidate the data associated with a key, or even update the local cache directly for immediate optimistic UI updates.

Server State Check

Which of the following are key benefits of using a library like SWR or React Query for managing server state?

Recap & Next Steps

You've explored the world of server state and learned why it requires special handling compared to client state. Libraries like SWR and React Query are indispensable tools for Next.js developers, simplifying data fetching, caching, revalidation, and error management.

By leveraging these libraries, you can build more performant, reliable, and user-friendly applications with less effort. Keep practicing these concepts to master your data fetching!

Sıkça Sorulan Sorular

“Sunucu Durumunu Yönetme” dersi ücretsiz mi?

Evet — “Sunucu Durumunu Yönetme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Next.js 15 Fullstack (App Router + Server Actions) kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Next.js 15 Fullstack (App Router + Server Actions) kursu toplamda 4 dersten oluşur.

“Sunucu Durumunu Yönetme” dersinde ne öğreneceğim?

SWR veya React Query dahil olmak üzere sunucudan alınan verileri yönetme ve bunları kullanıcı arayüzünüzle eşzamanlı tutma stratejilerini keşfedin. Next.js 15 Fullstack (App Router + Server Actions) ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Next.js 15 Fullstack (App Router + Server Actions) öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Next.js 15 Fullstack (App Router + Server Actions), başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.

“Sunucu Durumunu Yönetme” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Next.js 15 Fullstack (App Router + Server Actions) dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Next.js 15 Fullstack (App Router + Server Actions) dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. React Context API
  2. Zustand ile Genel Durum
  3. Sunucu Durumunu Yönetme
  4. Durumu localStorage'a Kalıcı Olarak Kaydetme
← Next.js 15 Fullstack (App Router + Server Actions) Sayfasına Dön