I will preface this by saying that everything works correctly, it's just that I'm trying to understand what is happening exactly and why I'm seeing this behavior.
Say I have a component, FooComponent, and two different routes, /foo and /bar. I'm rendering them as follows:
<Route
exact
path="/foo"
render={({ location }) => <FooComponent location={location} />}
/>
<Route
path="/bar"
render={({ location }) => <FooComponent location={location} />}
/>
When clicking between pages /foo and /bar, it seems that FooComponent mounts only once. To test this, here's how the FooComponent is implemented:
const FooComponent = ({ location }) => {
useEffect(() => {
console.log("Mount");
return () => {
console.log("Cleanup");
};
}, []);
return <div>At {location.pathname}</div>;
};
Mount is only logged once.
This completely puzzles me as FooComponent is rendered by two completely different parent Route components, and I thought React would only perform reconciliation on same parent component re-rendering with different child elements. Moreover, changing one to return <div><FooComponent location={location} /></div> and leaving the other as-is will cause the component to unmount.
Here is the reproducible example.