wouter icon indicating copy to clipboard operation
wouter copied to clipboard

[question] Parent route layout (<Outlet />)

Open gius opened this issue 2 years ago • 2 comments

I was not able to figure that out from the documentation. How do you handle a scenario when a parent route should render some layout with a child route inside? Similar to React Router's Outlet.

gius avatar Apr 10 '22 07:04 gius

Hi! It's currently not provided out of the box, however implementing <Outlet /> functionality shouldn't be hard. I'll try to come up with an example to illustrate my point.

molefrog avatar Apr 11 '22 06:04 molefrog

Thank you! Basically, it should be enough to figure out if a route is a parent of the current path.

E.g., that /finance and /finance/orders are parents of the current /finance/orders/123 path.

gius avatar Apr 11 '22 07:04 gius

this would be such a good feature to have built in. looking forward to the example @molefrog

PodaruDragos avatar Oct 14 '22 11:10 PodaruDragos

@gius @molefrog Here's an example of how this could be achieved out of the box without any "Outlet" (hint: React has its own built-in "outlet" called children). To reproduce the linked react-router example exactly (barring any silly coding mistakes I've made here since I did this off the cuff in about 30 seconds):

function Dashboard({children}: React.PropsWithChildren<{}>) {
  return (
    <div>
      <h1>Dashboard</h1>
      {children}
    </div>
  );
}

function App() {
  return (
      <Route path="/:_*">
          <Dashboard>
              <Route path="/messages"> // could also use a <Switch> here
                  <DashboardMessages/>
              </Route>
              <Route path="/tasks">
                  <DashboardTasks/>
              </Route>
          </Dashboard>
      </Route>
  );
}

There is literally no need for Outlet as far as I can tell. I always thought that was a pointless addition to react-router. Try as I might, I've never been able to come up with one single good explanation for why Outlet is necessary or desirable. Only serves to make function components less readable and entirely un-reusable outside of an outlet context (as well as adding the extra overhead of those gazillion useContext calls).

HansBrende avatar Nov 04 '22 04:11 HansBrende

I absolutely agree with @HansBrende, seems like outlets can be easily replaced with components. I understand why it is present in react-router: outlets can be found in some frameworks like Ember or Rails, so some devs might be familiar with this pattern.

I believe that wouter should not go down that route (pun intended), otherwise there is a risk of becoming 1-1 clone of react router. I like to compare it to Preact vs React. While Preact in its core has similar API as React, it does however leave aside some additional things like portals.

I'm closing this issue, thank you all for participating in this discussion.

molefrog avatar Nov 05 '22 15:11 molefrog