Zaid-Ajaj / Feliz.Router

A router component for React and Elmish that is focused, powerful and extremely easy to use.
MIT License
78 stars 16 forks source link

Nested routers #43

Closed padzikm closed 3 years ago

padzikm commented 3 years ago

Hi, do you support nested routers? ex - top router handles /resource and in rendered component there are further definitions for /resource/show or /resource/edit. That way it's easy to manage http requests for resource data to show / edit / etc

Zaid-Ajaj commented 3 years ago

You don't need nested routers. Instead, you can pass parts of the URL down to child components. Here is an example: consider you have these routes:

The top-level router will perform pattern matching like this

match currentUrl with
| [ ] -> Index()
| [ "contacts" ] -> Contacts()
| "user" :: userParts -> UserRouter(userParts)
| _ -> NotFound()

Notice here the pattern "user" :: userParts will match a route that starts with a segment called "user". Now UserRouter can take over and handle the parts

[<ReactComponent>]
let UserRouter(userUrl: string list) = 
  match userUrl with 
  | [ ] -> ShowAllUsers()
  | [ "show"; Route.Int userId ] -> ShowUser(userId)
  | [ "edit"; Route.Int userId ] -> EditUser(userId)
  | _ -> UnknownUserPage() 
padzikm commented 3 years ago

Great! thanks