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.

Using Fragments inside Switch

See original GitHub issue

React 16.2 added the concept of Fragments. I would like to be able to use Fragments inside Switch components to do things like this:

<Switch>
  <>
    <Route path="/" />
    <Route path="/about" />
  </>
  <>
    <Route path="/messages" />
    <Route path="/messages/new" />
  </>
</Switch>

The application I am working on is composed by small modules and all those small modules are composed together in a single app based on some conditions. Since routes need to be direct children of Switch those small modules need to expose routes as an array. However using fragments would make it a lot simpler and more readable.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
jamespleasecommented, Jan 12, 2018

I could see a case being made of not traversing the entire tree (which is clearly not going to work), but instead simply checking if a particular child is a Fragment. And, if it is, checking its immediate children. This helps with cases such as:

<Switch>
  {blah.map(b => (
    <>
      <routeA/>
      <routeB/>
      <routeC/>
    </>
  ))}
</Switch>

which seem like they will become pretty common as React 16 adoption grows. The alternative, using an array syntax with keys, is pretty ugly.

@timdorr, would you consider just looking inside of Fragments, or nah?

2reactions
timdorrcommented, Jan 22, 2018

Actually, I specifically don’t want to flatten sub-children. It should only check child.type === Fragment and then return child.props.children for processing in the loop. No recursion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redirect doesn't work inside Switch when Fragment has been ...
Fragment that has been used inside Switch . Redirection works fine when it's removed.
Read more >
React Router Switch + Fragments example - CodeSandbox
React Router Switch + Fragments example - CodeSandbox. You need to enable JavaScript to run this app.
Read more >
How to get React Router 4 to allow nested components inside ...
Here was my unsuccessful first-attempt using fragments: import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; ...
Read more >
Fragments - React
A common pattern in React is for a component to return multiple elements. Fragments let you group a list of children without adding...
Read more >
Understanding React fragments - LogRocket Blog
Learn how to use React fragments to prevent rendering unnecessary nodes in the DOM and create an optimized code.
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