Vulcan icon indicating copy to clipboard operation
Vulcan copied to clipboard

Layout remounts on navigation

Open jimrandomh opened this issue 5 years ago • 6 comments

Starting with Vulcan 1.13, due to the switch to react-router-4, Layout remounts on every navigation event. This means that any route-independent state on Layout or any of its route-independent children, it will be reset on navigation.

For us, this is a significant regression since we have route-independent navigation with state (eg a notification tray) which gets remounted, as well as some assorted important globals (the rendered timezone, an optimistic-update cache for a case that Apollo otherwise couldn't handle).

jimrandomh avatar Jul 24 '19 22:07 jimrandomh

Related to: https://stackoverflow.com/questions/42862028/react-router-v4-with-multiple-layouts

Not sure yet how to handle this. Maybe identifying automatically registered routes that got the same layout, and creating a group of routes for each layout (as stated in the new solution of this comment https://stackoverflow.com/a/42863173/5513532).

eric-burel avatar Jul 31 '19 08:07 eric-burel

Also can this state be moved in an App wrapper instead? Or maybe you need to switch it/reset it on certain route changes?

eric-burel avatar Jul 31 '19 08:07 eric-burel

We fixed this for our own use case by almost-completely ripping out react-router-v4 and implementing our own routing. However, this came at the expense of being able to use withRouter (as opposed to our own context providers), and probably also some other API subtleties of Vulcan. See https://github.com/LessWrong2/Lesswrong2/blob/devel/packages/vulcan-core/lib/modules/components/App.jsx .

(There's a broader lesson here, which is that architectural churn is much more expensive for downstream projects than you think. From my perspective, react-router-v4 was an unwelcome imposition that cost us a lot of time for no benefit.)

jimrandomh avatar Jul 31 '19 15:07 jimrandomh

It seems that this comment could help us to avoid remounting when components share the same layout: https://stackoverflow.com/a/57358789 I haven't tested much but it seems to be functional. For us that would mean parsing the route array once to group route per layout. However you will have a remount whenever the layout change even with this technique.

eric-burel avatar Oct 08 '19 19:10 eric-burel

This seems relevant? https://adamwathan.me/2019/10/17/persistent-layout-patterns-in-nextjs/

SachaG avatar Oct 21 '19 06:10 SachaG

Fun fact: I was going to paste the same article, almost a year later. Yeah it's completely relevant and technically that's what we should do in Vulcan.

So my PR is the "naive" pattern of remounting only on layout change, and the current implementation is the layout that remount systematically

eric-burel avatar Jul 30 '20 08:07 eric-burel