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

useSWR not working with server components. Error: Unsupported server component type: {...} #31652

Closed
saurabhguptarock opened this issue Nov 21, 2021 · 3 comments
Labels
bug Issue was opened via the bug report template.

Comments

@saurabhguptarock
Copy link

saurabhguptarock commented Nov 21, 2021

What version of Next.js are you using?

12.0.5-canary.5

What version of Node.js are you using?

14.18.1

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

Hooks are not supported in server components.
useSwr not working with server components using suspense.

Expected Behavior

The component should not throw error

To Reproduce

const { data: coupons } = useSwr<CouponsProps[]>(
    "coupons",
    () => getCoupons(),
    { suspense: true }
  );
@saurabhguptarock saurabhguptarock added the bug Issue was opened via the bug report template. label Nov 21, 2021
@lachlanjc
Copy link
Contributor

SWR cannot be used in server components: #42249 (comment)

@shuding
Copy link
Member

shuding commented Nov 10, 2022

Currently, SWR is designed for client-side data fetching (can be used in a client "use client" component). It leverages React's Context, State, Effect, etc. to achieve the stale-while-revalidate and caching experience. All those are not necessary in a server only environment, and you should directly go with the use hook there (check related docs).

@shuding shuding closed this as completed Nov 10, 2022
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

4 participants