A declarative client-side router for Vecty applications. Similar to react-router v4
go get marwan.io/vecty-router
You don't need to declare your routes at the top level. You can declare them inside any component and if they match they will render, otherwise, router will render an empty div instead.
package components
import (
"github.com/hexops/vecty"
"github.com/hexops/vecty/elem"
"marwan.io/vecty-router"
)
// Body renders the <body> tag
type Body struct {
vecty.Core
}
// Render renders the <body> tag with the App as its children
func (b *Body) Render() vecty.ComponentOrHTML {
return elem.Body(
router.NewRoute("/", &MainView{}, router.NewRouteOpts{ExactMatch: true}),
router.NewRoute("/blog", &Blog{}, router.NewRouteOpts{}),
router.NewRoute("/blog/{id}", &PostView{}, router.NewRouteOpts{ExactMatch: true}),
)
}
To retrieve a named variable like {id} in the example above you can do
// Render returns every title
func (pv *PostView) Render() vecty.ComponentOrHTML {
id := router.GetNamedVar(pv)["id"]
return elem.Div(
vecty.Text(id),
)
}
func (c *component) Render() vecty.ComponentOrHTML {
return elem.Span(
router.Link("/my/route", "click here", router.LinkOptions{}),
)
}
router.Redirect("/my/route")
Currently vecty-router does not fallback to hash routing if the History API is not on your browser. It also calls vecty.Rerender on all routes whenever a route changes. It should/will do its own deducing of whether to call rerender on a route or not based on route matches and whether it's already mounted or not.