Skip to content

Latest commit

 

History

History
76 lines (56 loc) · 1.86 KB

README.md

File metadata and controls

76 lines (56 loc) · 1.86 KB

Vecty Router

A declarative client-side router for Vecty applications. Similar to react-router v4

Installation

go get marwan.io/vecty-router

Usage

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),
	)
}

Other features

Navigation through links
func (c *component) Render() vecty.ComponentOrHTML {
	return elem.Span(
		router.Link("/my/route", "click here", router.LinkOptions{}),
	)
}
Programatically navigate to a route
router.Redirect("/my/route")

Status

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.

Alternatives