router icon indicating copy to clipboard operation
router copied to clipboard

Error when using Shadcn Button in Server Components

Open josefbender opened this issue 7 months ago • 4 comments

Which project does this relate to?

Start

Describe the bug

When using a Shadcn Button in a component rendered using renderServerComponent there is an error:

React.createContext is not a function

Your Example Website or App

https://github.com/josefbender/rsc-shadcn-button-error

Steps to Reproduce the Bug or Issue

  1. Create a component using Button from shadcn/ui with Radix
  2. Render the component using renderServerComponent (eg. in a server function)
  3. See error: React.createContext is not a function

Expected behavior

That the Button can also be rendered on the server.

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.167.4 / 1.166.15
  • OS: macOS
  • Browser: Chrome
  • Browser Version: 147.0.7727.56
  • Bundler: vite
  • Bundler Version: 7.3.1

Additional context

No response

josefbender avatar Apr 21 '26 14:04 josefbender

Hey have you tried sticking a use client to the top of your Button.tsx ?

nikilok avatar Apr 21 '26 14:04 nikilok

You might also just want to return <PostContent ... /> directly here without the renderServerComponent(..), but if you do want to execute it on the server, then marking Button with 'use client' turns it into a client reference in the RSC graph, so Radix's createContext call never evaluates in the react-server environment (which is what's actually throwing).

nikilok avatar Apr 21 '26 14:04 nikilok

also make sure to set rsc to true in the components.json

SkandarS0 avatar Apr 23 '26 03:04 SkandarS0

Interesting... Mark only just the Button as 'use client' ?

isocroft avatar Apr 24 '26 20:04 isocroft