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.

[Discussion] Usefulness of the Switch

See original GitHub issue

This is a kinda spin-off from #5785.

Current state

Currently, the react-router is built so we can have routes all over the app and don’t bother much with static routing configuration. That’s definitely great concept, but there is one flaw.

The Switch most common use case is to have a fallback route that should be rendered when nothing else matches. Apparently, that doesn’t work well with dynamic routing nature. It forces us to have all routes in a single place again to try match against all of them till it reaches the fallback one.

Another issue is in case I simply want to wrap a single Route to another component because there is some additional logic within the route that would clutter whole Switch

<Switch>
  <TooSmartRoute /> // notice no props here, everything is inside the component
</Switch>

Such route is always matched by the Switch simply because it’s lacking path in the props.

Solution?

Letting Switch to traverse the whole tree of children to find a match is definitely a bad idea.

Honestly, I don’t have any proven solution, just a hot idea. It would be something along the lines of having a new component called eg. Fallback which would accept prop of the fallback route.

<Fallback route={<NotFound />}>
  // ... some deeply nested routing
</Fallback>

This component would just render everything without any traversal. When some Route would match, it would internally use context to inform Router about that. Then the Fallback would be able to pick up that information (not sure if that’s possible actually) and render fallback route if there wasn’t any match.

Any other thoughts/ideas?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
timdorrcommented, Jan 31, 2018

While you might be able to build something like this now, this will essentially be impossible when async rendering drops in React. Namely because you can never know when the state of all Routes within a tree are fully materialized. Routes can pop in and out of the tree at any moment asynchronously. And the lifecycle methods to synchronize them (cWM, cWRP) are going away. So you’ll end up with weird synchronization issues, mostly in the form of trailing content in the DOM while the render loop finishes processing and reconciling.

This is going to be an interesting transition for most apps and libraries, where certain guarantees are no longer possible.

Really the only solution here will be to go back to the old way of doing things via a top-level route configuration, aka react-router-config. That can manage the config internally and stays synchronous within its own lifecycle (async doesn’t do any preemption). I’d like to get it out of beta and close out things like #5429 and #4962. It can solve a lot of these problems, but it needs some more superpowers to be truly useful.

0reactions
edkalinacommented, Jun 3, 2018

It was announced that React Call Return won’t be released. So I’ve implemented similar pattern as react-aggregation library.

Here is concept of new Switch implemented with react-aggregation. It allows to nest routes any way you want.

What do you think about this? @FredyC @timdorr @jamesplease

Read more comments on GitHub >

github_iconTop Results From Across the Web

Book Discussion Questions for Beth O'Leary The Switch
Book Discussion Questions for Beth O'Leary. The Switch. 1. What is the significance of the title? Would you have given the book a...
Read more >
We Played the Switch! Hands-On Impressions Discussion
We played the Nintendo Switch as well as a ton of games including Splatoon 2, Zelda: Breath of the Wild, Mario Kart 8...
Read more >
Nintendo Switch - Reveal Discussion (Thoughts & Concerns)
Nintendo has revealed the Switch ! We go over our impressions including the unveiling itself, it being a hybrid, controls, battery life, ...
Read more >
Is the Switch a BORING Console? | DISCUSSION - YouTube
We recorded this back in December so whenever we say something that's out of date now you know whyJoin the official Back Log...
Read more >
SWITCH LITE DISCUSSION - Pros and Cons! - YouTube
TWITCH - http://twitch.tv/BalrogGameRoomSTREAM ARCHIVE - http://youtube.com/BalrogsGameplayRoomTWITTER ...
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