Course contentsShow
← React
Lesson 1586 of 2,39639. Server Components in PracticeFree lesson

Mental Model: Server-Side useEffect

Think of async server components as equivalent to useEffect with no dependencies, but on the server at build/request time.

Mental Model: Server-Side useEffect

What you'll learn: How to think about async server components as the server-side equivalent of useEffect with no dependencies.

The Core Analogy

In client-side React, you've used useEffect(() => { ... }, []) countless times to fetch data when a component first mounts. The empty dependency array means "run once on mount, then never again."

Async server components work with the exact same mental model, except they run on the server at build time or request time instead of in the browser:

// Client Component - old pattern
function ClientPost({ id }) {
  const [post, setPost] = useState(null);
  
  useEffect(() => {
    fetch(`/api/posts/${id}`)
      .then(res => res.json())
      .then(setPost);
  }, []); // "Run once on mount"
  
  return <div>{post?.title}</div>;
}

// Server Component - new pattern
async function ServerPost({ id }) {
  const post = await db.posts.findById(id); // "Run once at render time"
  
  return <div>{post.title}</div>;
}

Both patterns share the same intent: fetch data once when the component renders. The difference is where and when that "once" happens.

The Key Difference

With useEffect, "once" means once per client mount. With async server components, "once" means once per server render (build time for static pages, or request time for dynamic pages). There's no re-running, no cleanup functions, no dependency tracking—just straightforward top-to-bottom execution.

Key Takeaway: Think of async server components as useEffect with an empty dependency array that runs on the server instead of the client—data fetching becomes a simple, synchronous-looking operation that happens exactly once per render.