question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When a React RouteNode should re-render?

See original GitHub issue

Hello @troch! I’m having a big doubt after a bug emerged in my react-mobx plugin.

My version of RouteNode is based on yours react-router5 version .

Now I’ve noticed that in your version whenever a route change happens all your RouteNodes will re-render. This usually works when your component structure resemble exactly a single tree and of course the visual changes will only happen in the children branches of the intersection RouteNode (because of the switch or any other means to select the sub-components).
Nonetheless all RouteNodes are being re-rendered.

I think that this might be inefficient and I put in place a logic to re-render only the intersection node. In this case when the component structure is a tree it works because all the subcomponent of the intersection are re-generated and so any RouteNode in this branch will be re-generated and will select the correct subcomponents even tough it’s not the intersection (this is a render, not a re-render).

Now this logic turned to be buggy for some specific cases (example: app.a({id:1}).b -> app.a({id:2}).c) so I tough to add an extra condition:

Re-render a RouteNode only when:

  • it represents the intersection node
  • or the segment associated with RouteNode is one of the segments toActivate

I tough preventing re-rendering RouteNodes might be necessary when you have a lot of them and only few of them really need to. Or when your component structure is organized in multiple parallel trees for example.

What do you think? But most important, am I overcomplicating this and I should just trust React and its internal algorithms? 😆

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
trochcommented, Apr 19, 2018

That is a routeNode should update either when it is the intersection node or it is one of the node to activate.

I think I can refine it. It will reload if:

  • If it is the intersection node
  • Going from the intersection node to leaves, any activated node which has a matching deactivated node, until not (it makes sense in my head anyway 😛 ).
1reaction
trochcommented, Apr 9, 2018

I guess that makes sense since when nesting routeNode HoCs, children route nodes of an updating route node don’t get local state updates. I think I need to introduce a key in routeNode or other fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React doesn't rerender when redirecting to same route
I have a react/node app that shows different data on tv monitors all going to different urls. localhost/s/1... localhost/s/2, etc. We want to ......
Read more >
5 Ways to Avoid React Component Re-Renderings
1. Memoization using useMemo() and UseCallback() Hooks ... Memoization enables your code to re-render components only if there's a change in the props....
Read more >
Reconciliation - React
Just to be clear, rerender in this context means calling render for all components, it doesn't mean React will unmount and remount them....
Read more >
Common Mistakes React Developers Make – And How to Fix ...
Whenever you're using routing in a React application, you declare routes inside the Switch component. This means that only one component with ...
Read more >
React Router basics: What I wish they'd told me - Medium
You can put your routes in just like any other React components, ... If you use the URL bar in the browser, your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found