Consider the following code:
App.jsx:
import ContextProvider from "./provider/contextProvider";
import Routes from "./routes";
function App() {
console.log("In App");
return (
<ContextProvider>
<Routes />
</ContextProvider>
);
}
export default App;
Routes.jsx:
import { RouterProvider, createBrowserRouter } from "react-router-dom";
import { provideContext } from "./provider/contextProvider";
import Component1 from "./pages/Component1";
import Component2 from "./pages/Component2";
const Routes = () => {
const { token } = provideContext();
const router = createBrowserRouter([
{
path: "/Component2",
element: <Component2 />,
},
{
path: "/Component1",
element: <Component1 />,
},
]);
return <RouterProvider router={router} />;
};
export default Routes;
pages/Component1.jsx:
const Component1 = () => {
console.log("In Component1");
return <div>Component1</div>;
};
export default Component1;
pages/Component2.jsx:
import { useNavigate } from "react-router-dom";
import { provideContext } from "../provider/contextProvider";
const Component2 = () => {
const navigate = useNavigate();
const { setToken } = provideContext();
console.log("In Component2");
const onClick = () => {
setToken("Token");
setTimeout(() => {
navigate("/Component1");
});
};
return (
<>
<button onClick={onClick}>Click me</button>
</>
);
};
export default Component2;
provider/ContextProvider.jsx:
import { createContext, useContext, useState } from "react";
const Context = createContext();
const ContextProvider = ({ children }) => {
const [token, setToken] = useState("Initial");
return (
<Context.Provider value={{ token, setToken }}>{children}</Context.Provider>
);
};
export const provideContext = () => {
return useContext(Context);
};
export default ContextProvider;
When clicking the button in Component2
, the URL changes but the UI still shows Component2.
Component 1
is not even rendered once.
Interestingly, if i remove the setTimeout()
in Component2.jsx
function OR remove const { token } = provideContext();
in Routes.jsx
, the issue goes away. Not sure what is going.