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

match object is always newly created #5099

Closed
greenimpala opened this issue May 11, 2017 · 9 comments
Closed

match object is always newly created #5099

greenimpala opened this issue May 11, 2017 · 9 comments

Comments

@greenimpala
Copy link
Contributor

<Route> components pass props.match as a fresh object when the URL / params have not changed. Is this by design? This was not the case with RR V3. This breaks the PureComponent paradigm. See codepen below for example.

Version

4.0.0

Test Case

https://codepen.io/anon/pen/NjYwZO?editors=1112

Steps to reproduce

Render a PureComponent within a Route.

Expected Behavior

If the URL has not changed then re-renders of the Route should be passed the same props.

Actual Behavior

match is always a new object causing the component to be re-rendered. Even though its contents are unchanged.

@timdorr
Copy link
Member

timdorr commented May 11, 2017

Yes, this is intentional. You should implement your own sCU to use the semantics you prefer, checking on the field values that matter to your component's updating.

Simple equality could be misleading, because the match object's properties could change if we re-used the object. Also, it would be messy to track the existing object because computeMatch would need to keep it around and there can be multiple matches within the same tree.

@timdorr timdorr closed this as completed May 11, 2017
@greenimpala
Copy link
Contributor Author

greenimpala commented May 12, 2017

It's impossible to safely implement an sCU to take care of this because child routes may use arbitrary params. If a higher level route component only cares about /:id and ignores match / match.params then this blocks lower level routes from rendering when the URL changes to {props.match.url}/:subid for example.

the match object's properties could change if we re-used the object.

In that case you'd make a new Object - you just memoize matchPath in Route.js from its input (pathname, path, strict, exact).

@timdorr I am happy to submit a PR if you believe this should be handled in the RR4 API.

@mattkrick
Copy link
Contributor

This was a huge gotcha, it's causing an unnecessary rerender on a handful of components.

Simple equality could be misleading, because the match object's properties could change if we re-used the object

@timdorr could you give some info on this? I assume match is treated as an immutable internally, and if folks mutate it in their app, then they shouldn't expect an update, right?

i think @greenimpala has a good idea in the memoization. @timdorr could you confirm if that'd be an acceptable PR?

@maierson
Copy link

Any news on this? It's causing us unnecessary re-renders on our PureComponent main view invalidating all our immutability efforts.

@timdorr Is there at least a way to prevent the Route from injecting the match object into the component's props?

@imarotte
Copy link

imarotte commented Oct 9, 2017

I'd also love additional info on this @timdorr

There has to be another alternative than writing massive amounts of sCUs. That just isn't practical for large scale projects

@shahzad31
Copy link

Any update on this issue, i just started optimising my app and i found this to be a big problem for me.

@pealmeid
Copy link

Same here, just ran into this issue when I was optimizing a project. Any updates? Thanks in advance.

@shahzad31
Copy link

well what i have done is don't pass whole match object , just extract whatever prop you need in child component and pass that.

@pealmeid
Copy link

@shahzad31 thanks for the quick reply, can you give more information? At what moment in the component's life cycle are you extracting the props?

@lock lock bot locked as resolved and limited conversation to collaborators Sep 21, 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

7 participants