I have a connected (to a redux store) component. I have multiple routes that all use this component in a render prop. On every route change, the entire component appears to be re-mounted. Is there any way to prevent this from happening? My first thought was that I may be re-instantiating the component every time, due to the render prop being a function that react router calls? Here's some code:
const routeFunction =
(resource, props) => <CrudWrapper resource={resource} modelId={props.match.params.id} />
export default crudResources.map(resource => ({
path: `/${resource}/:id?`,
link: `/${resource}`,
resource,
// eslint-disable-next-line react/prop-types
render: routeFunction.bind(null, resource)
}));
The routes are then mapped in JSX:
{routes.map(route => <Route key={route.path} {...route} />)}
I had the routes wrapped in a <Switch>
and thought this might be causing the issue, but even after removing the switch, the component's componentWillMount
method is called every time.