frontarm / navi

🧭 Declarative, asynchronous routing for React.
https://frontarm.com/navi/
MIT License
2.07k stars 71 forks source link

Help needed: nested views for some routes #177

Closed haikyuu closed 4 years ago

haikyuu commented 4 years ago

Hi, thanks for this beautiful library.

I'd like to implement a basic layout in my app:


export default compose(
  mount({
    "/login": route({
      title: "Login",
      view: <Login />,
    }),
    "/": compose(
      withView((request) => (
        // This is the first view -- it renders the second within a
        // `<Layout>` component.
        <Layout request={request} mountpath={request.mountpath || "/"}>
          <View />
        </Layout>
      )),
      mount({
        "/home": route({
          title: "Home",
          getData: () => api.get("cases").json(),
          view: <Home />,
        }),
      })
    ),
  })
);

I get /home not found error. Can you help me fix this issue?

jamesknelson commented 4 years ago

Glad you like the library!

When you want to pass through all unmatched routes from a mount, then instead of using /, you'll want to use a * wildcard:

export default compose(
  mount({
    "/login": route({
      title: "Login",
      view: <Login />,
    }),
    "*": compose(
      withView((request) => (
        // This is the first view -- it renders the second within a
        // `<Layout>` component.
        <Layout request={request} mountpath={request.mountpath || "/"}>
          <View />
        </Layout>
      )),
      mount({
        "/home": route({
          title: "Home",
          getData: () => api.get("cases").json(),
          view: <Home />,
        }),
      })
    ),
  })
);

Hope this helps!

haikyuu commented 4 years ago

Works like a charm. Thanks