I'm having an issue with a component that is rendering from an external site that is rendering twice, I'm using react 18 and I already use a ref to handle the strict mode and new useEffect behavior, but i can't figure out why my dispatch is executing twice. I have removed everything from the component leaving only the log and I still get the same problem.
Routes:
return ( <Grid> <PersistGate loading={null} persistor={persistor}> <SidebarProvider> <Routes> <Route path="/" element={<Welcome />} /> <Route path="oauth2-callback" element={<Oauth />} /> <Route element={<ProtectedRoutes />}> <Route element={<Layout />}> <Route path="/home" element={<Home />} /> <Route path="/client-view/*" element={<Submission />} /> </Route> </Route> </Routes> </SidebarProvider> </PersistGate> </Grid> ); }; export default App; Component:
import { useSearchParams } from "react-router-dom"; import { useAppDispatch } from "../../store/hooks/hooks"; import { getOauth } from "../../store/thunks/app/app.thunks"; const Oauth = () => { const dispatch = useAppDispatch(); const shouldDispatch = useRef(true); const [searchParams] = useSearchParams(); useEffect(() => { if (shouldDispatch.current) { shouldDispatch.current = false; dispatch( getOauth({ code: searchParams.get("code") || "", state: searchParams.get("state") || "", }) ); } }, []); return null; }; export default Oauth; URL: https://localhost:3000/oauth2-callback?state=dsada2321&code=12345
React.StrictModecomponent. The solution is to properly cleanup any side-effects.