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

React useAuth / useUser context don't work in Vite projets (HMR issue) #2144

Closed
4 tasks done
AndreasGalster2 opened this issue Nov 16, 2023 · 2 comments
Closed
4 tasks done
Labels
linear Created by Linear-GitHub Sync

Comments

@AndreasGalster2
Copy link

AndreasGalster2 commented Nov 16, 2023

Preliminary Checks

Reproduction / Replay Link

https://stackblitz.com/edit/vitejs-vite-jnusqg?file=src%2FApp.tsx,package.json&terminal=dev

Publishable key

pk_test_Y2xldmVyLXNxdWlycmVsLTU5LmNsZXJrLmFjY291bnRzLmRldiQ

Description

Steps to reproduce:

https://stackblitz.com/edit/vitejs-vite-jnusqg?file=src%2FApp.tsx,package.json&terminal=dev

  1. Install Vite (I used React template)
  • npm create vite@latest my-react-app -- --template react
  1. Copy / paste basic React example from the docs with useAuth or useUser context
  2. Cannot find context on page load

Expected behavior:

No errors and being able to retrieve the auth / user context

Actual behavior:

Error: UserContext not found
    at assertContextExists

I only briefly looked into the issue. This used to be a HMR issue on Vite for a while that Context wouldn't work, but it's been fixed roughly a year ago. I assume this has to do with the fact that there are some custom wrappers for Context Creation. I tried a normal CreateContext / useContext example in the same app and it works without issues.

PS: I'm not confirming it's a HMR issue. I just stumbled upon this issue vitejs/vite#3301 which appears to be very similar, and I assume since useAuth / useUser are React context, it must be related.

Environment

System:
    OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 5.01 GB / 7.62 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 18.18.2 - ~/.nvm/versions/node/v18.18.2/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.18.2/bin/yarn
    npm: 10.2.0 - ~/.nvm/versions/node/v18.18.2/bin/npm
    pnpm: 8.10.5 - ~/.nvm/versions/node/v18.18.2/bin/pnpm
    Watchman: 2023.10.09.00 - /home/linuxbrew/.linuxbrew/bin/watchman
  npmPackages:
    @clerk/clerk-react: ^4.28.0 => 4.28.0
    @types/react: ^18.2.15 => 18.2.37
    @types/react-dom: ^18.2.7 => 18.2.15
    @typescript-eslint/eslint-plugin: ^6.0.0 => 6.11.0
    @typescript-eslint/parser: ^6.0.0 => 6.11.0
    @vitejs/plugin-react: ^4.1.1 => 4.1.1
    eslint: ^8.45.0 => 8.53.0
    eslint-plugin-react-hooks: ^4.6.0 => 4.6.0
    eslint-plugin-react-refresh: ^0.4.3 => 0.4.4
    react: ^18.2.0 => 18.2.0
    react-dom: ^18.2.0 => 18.2.0
    react-router-dom: ^6.18.0 => 6.18.0
    typescript: ^5.0.2 => 5.2.2
    vite: ^4.5.0 => 4.5.0
@AndreasGalster2 AndreasGalster2 added the needs-triage A ticket that needs to be triaged by a team member label Nov 16, 2023
@jescalan jescalan added the linear Created by Linear-GitHub Sync label Nov 16, 2023
@jescalan
Copy link
Contributor

Hey @AndreasGalster2 - we looked into this and noticed that you are calling useUser outside the context of the <ClerkProvider> component. In order for useUser to render correctly, it needs to be called inside a component that is wrapped with <ClerkProvider>.

We certainly could do a better job with this error message, and are adding a task for this in our internal backlog, but this should be able to be resolved in your app by adjusting the scope of <ClerkProvider> right away 😁

@AndreasGalster2
Copy link
Author

Ohh, how did I not see that? I should have definitely caught that, my bad, thanks a lot for the help!

@LekoArts LekoArts removed the needs-triage A ticket that needs to be triaged by a team member label Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear Created by Linear-GitHub Sync
Projects
None yet
Development

No branches or pull requests

3 participants