I am using react router v4 and react v16. There is a need where some components be mounted only once (when rendered first time), but rendered based on the Router path matches. (One such use case is to show a report for a menu item click, and at a later invocation, the report shown need not be fetched again).
Using Switch or just plain Route component -- the component gets unmounted, when that component is not in the path.
Currently, I am doing like this (which solves the issue in hand):
<main>
<Route exact path="/" component={Home} />
<WrapRoute exact path="/about" component={About} />
</main>
const WrapRoute = ({path, ...rest}) => (
<div style={{display: rest.location.pathname == path ? "block" : "none"}}>
<Route {...rest} />
</div>
)
But I don't think directly verifying the location.pathname is a good idea. I would like 'Route' component to handle this.
I would like suggestions in handling this scenario (in such a way that the component is not re-mounted again). A solution or a different pattern, to address this is welcome.