React & Next.js errors · Error 05 of 7

Next.js: “needs useState … only works in a Client Component”

By Muhammad Abdullah, full-stack, mobile and ai automation developer · Last reviewed

You're importing a component that needs useState. This React Hook only works in a Client Component.

Short answer

In the Next.js App Router, components are Server Components by default and cannot use hooks or browser APIs. Add "use client" at the top of the file that uses them, and keep that client component as small as possible.

Common causes

  • Using useState, useEffect, event handlers or browser APIs in a Server Component.

How to fix it

  1. Mark the interactive component:

                          "use client";
    
    import { useState } from "react";
                        
  2. Keep data fetching in the server component and pass the data as props to the small client component.

Let’s talk about the next step

Have an idea, a stuck app, or a workflow toimprove?

Tell me what you are building, what is blocking you, and what a successful outcome looks like.