Skip to content

React Hooks issue with react-router-v6 integration #5309

Description

@alexabidri

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/react

SDK Version

7.2.0

Framework Version

7.2.0

Steps to Reproduce

1. Starting a react application with Vite.js in dev mode with react-refresh
2. Adding a simple Application like this one

importReactfrom'react';import{createRoot}from'react-dom/client';import{BrowserRouter,useLocation,useNavigationType,createRoutesFromChildren,matchRoutes,Routes,Route}from'react-router-dom';import*asSentryfrom'@sentry/react';import{BrowserTracing}from'@sentry/tracing';import{APP_ENVIRONMENT,SENTRY_DSN}from'./config';Sentry.init({dsn: SENTRY_DSN,integrations: [newBrowserTracing({routingInstrumentation: Sentry.reactRouterV6Instrumentation(React.useEffect,useLocation,useNavigationType,createRoutesFromChildren,matchRoutes),}),],environment: APP_ENVIRONMENT,tracesSampleRate: 1.0,});constSentryRoutes=Sentry.withSentryReactRouterV6Routing(Routes);functionrender(){constcontainer=document.getElementById('root');if(!container){returnnull;}constroot=createRoot(container);root.render(<React.StrictMode><BrowserRouter><SentryRoutes><Routepath="/"element={<React.Fragment/>}/></SentryRoutes></BrowserRouter></React.StrictMode>,);}(()=>{render();})();

Same code is in the official doc of Sentry https://docs.sentry.io/platforms/javascript/guides/react/configuration/integrations/react-router/
3. The app starts normally in dev mode.
4. As soon as I perform one modification, the front-end application crash completely. I need to close the tab and stop the application.

When I remove

new BrowserTracing({
routingInstrumentation: Sentry.reactRouterV6Instrumentation(React.useEffect, useLocation, useNavigationType, createRoutesFromChildren, matchRoutes)
})

from the sentry integrations, everything goes back to normal

Expected Result

When I do a change in local while I am developing, I should see my changes in live without my app crashing. I should be able to use BrowserTracing with Sentry without my app having issues

Actual Result

Warning: React has detected a change in the order of Hooks called by SentryRoutes. This will lead to bugs and errors if not fixed. For more information, read the Rules of Hooks: https://reactjs.org/link/rules-of-hooks

There is issue in the order of hooks execution while adding the BrowserTracing from Sentry.
Screenshot 2022-06-24 at 18 19 44

Vite.js config to start the app.

import{defineConfig,loadEnv}from'vite';import{visualizer}from'rollup-plugin-visualizer';importreactfrom'@vitejs/plugin-react';importsvgrPluginfrom'vite-plugin-svgr';exportdefaultdefineConfig(({ mode })=>{constenv=loadEnv(mode,process.cwd());// expose .env as process.env instead of import.meta since jest does not import meta yetconstenvWithProcessPrefix=Object.entries(env).reduce((prev,[key,val])=>{return{
...prev,['process.env.'+key]: `"${val}"`,};},{});return{server: {port: 3000,},preview: {port: 8080,},define: envWithProcessPrefix,build: {sourcemap: true,outDir: 'build',minify: 'esbuild',},plugins: [react(),svgrPlugin({svgrOptions: {// ...svgr options (https://react-svgr.com/docs/options/)},}),visualizer(),],};});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions