Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render prop remounts the component on every route change #5972

Closed
trassmann opened this issue Feb 27, 2018 · 5 comments
Closed

render prop remounts the component on every route change #5972

trassmann opened this issue Feb 27, 2018 · 5 comments

Comments

@trassmann
Copy link

Version

4.2.0

Test Case

https://codesandbox.io/s/j2p1r0kvx5

Expected Behavior

According to the documentation here: https://reacttraining.com/react-router/web/api/Route/render-func the inline render with a render prop should not remount the component every time.

Actual Behavior

You can see from the console logs that the Test component is unmounted and mounted again for every route change.

I have been trying to find a reason for this since yesterday. But even in the simplest test case this still happens. Please excuse my lack of knowledge, if this is not a bug, please explain what is happening.

@pshrmn
Copy link
Contributor

pshrmn commented Feb 27, 2018

You need to wrap your <Route>s in a <Switch> to get the behavior that you are expecting. A <Switch> will only render the first matching <Route>, while your implementation renders all three.

@pshrmn pshrmn closed this as completed Feb 27, 2018
@trassmann
Copy link
Author

@pshrmn Thank you Paul. But then there is still this issue: https://codesandbox.io/s/ywzll8wn8j

As soon as I map the routes from an array and i add a key prop, the same faulty behaviour appears, the component is constantly remounted. If you remove the key prop in the sandbox, the faulty behaviour stops. Is there a solution for this as well?

@pshrmn
Copy link
Contributor

pshrmn commented Feb 27, 2018

That is just how React's reconciliation works. React compares an element's type and its key and if either of those are different, it unmounts the current component and mounts a new component.

If #5889 gets merged, you will be able to group multiple paths under the same <Route>, so that might fit your use case better.

@trassmann
Copy link
Author

But the key does not change in this example, no?

@trassmann
Copy link
Author

Scratch that. I misunderstood. Makes perfect sense. Thank you Paul, sorry for the useless bug report.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants