maxence-charriere / go-app

A package to build progressive web apps with Go programming language and WebAssembly.

Home Page:https://go-app.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to do navigation in UI

suntong opened this issue · comments

I know there is a ctx.Navigate, but how can I do app.Navigate("/thing") when defining UI?

Quote from #412:

I call app.Navigate("/thing")

ctx.Navigate is typically called from a user event like a button click

func (r *ComponentOne) Render() app.UI {
	return app.Button().Text("go to two").OnClick(func(ctx app.Context, e app.Event) {
		ctx.Navigate("/two")
	})
}

Ah, yes!

Now, how to fix this app.Navigate please?

func (c *city) OnNav(u *url.URL) {
	key := u.Query().Get("city")
	if key == "" {
		key = "paris"
	}

	data, ok := cityStore[key]
	if !ok {
		// app.Navigate("/notfound")
		return
	}
	c.data = data
	c.Update()
}

V9 of OnNav is passed an app.Context. V6 was passed *url.URL.

// Navigator is the interface that describes a component that can perform
// additional actions when navigated on.
type Navigator interface {
	Composer

	// The function that called when the component is navigated on. It is always
	// called on the UI goroutine.
	OnNav(Context)
}

Ops, yes, thanks!