You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've tested this code with clear cra typescript template to make sure none of dependencies or app logic affects this behavior but a bug described below still occurs there.
It's not even about having useLocation as a dependency in useEffect inside ProtectedRoute to make ProtectedRoute log each time we switch the route of the app (I mistook it above - I mean toggling between "/" and "/chat" paths).
I don't want to force it to re-render.
Shouldn't the default behavior of Routes component be to just render anew the whole element of Route each time the route of the app matches the Route's path??
Consider these two scenarios:
When I pass element prop as a JSX.Element (the first code example from Actual Behavior above):
it renders ProtectedRoute only once and fires its console.log from useEffect only once while running the entire application. During toggling routes, it stays rendered as if it had only inital render because its console.log does not fire anymore. Calling API doesn't work too. Only its children seems to fire their console.log from useEffect on every route change.
When I pass element prop as a function that returns JSX.Element (the second code example from Actual Behavior above):
it renders anew ProtectedRoute and its children every time I toggle routes. console.log inside useEffect of ProtectedRoute starts firing on every route change
Prove me wrong but I still see scenario no. 1 as a form of a bug. It should at least yall at me that passing nested JSX.Element isn't allowed because it can't handle it properly
What version of React Router are you using?
6.2.1
Steps to Reproduce
I've tested this code with clear cra typescript template to make sure none of dependencies or app logic affects this behavior but a bug described below still occurs there.
Expected Behavior
Component ProtectedRoute should fire its console.log from useEffect every time I switch between / and /profile routes.
It doesn't work because the ProtectedRoute isn't re-rendered.
Only those nested components within ProtectedRoute are re-rendered. (Profile and Chat )
Actual Behavior
When I pass element to the Route like that:
it only re-renders Profile component when switching routes: console.log from Profile component fires but console.log from ProtectedRoute does not.
When I pass it this way:
It start re-rendering both ProtectedRoute and Profile when switching between routes so both console.logs start working
The text was updated successfully, but these errors were encountered: