Main design goals:
-
using virtual dom, supports rendering on the server
-
hyperscript style api
H
as a core building block for the virtual dom (templating could based on this in the future) -
components start small, with simple functional approach, each component is a single function returns a
VNode
func Welcome(name string) *vdom.VNode { return h("h2", nil, h("Hello ", nil), h(name, nil)) }
Questions:
- when and how to trigger re-render on state changes?
- trigger manually from the component, with message passing for example
- trigger re-render on event handlers callback, how about non UI interactions like fetch/xhr?
- wire actions like hyperapp: in go reflection might involved with this approach
app:
package main
import (
".../vdom"
)
func main() {
app := NewApp()
app.Route('/', home)
app.Render("#root")
}
func home(state *State) *vdom.VNode {
return vdom.H("div", nil,
vdom.H("h1", nil, vdom.H(state.Message)),
)
}
server:
..
initState := &app.State{Count: 1}
view := app.View(initState, nil)
html := server.RenderToString(view)
..
client:
var state *app.State
initState := []byte(dom.Window.Get("initialState").String())
json.Unmarshal(initState, &state)
actions := &app.Actions{}
App(state, actions, app.View, dom.QuerySelector("#root"))