Introduction
fnComponent
Package fncmp is a project aimed to bring granular control to server-side rendering with Go with functionality such as maintaining state in functional components, event handling in HTML components.
Granular control
Developers can serve HTML components from a web server using packages such as templ or with fncmp's HTML struct. The FnComponent struct provides enhanced functionality to these components including when and how they are rendered to the DOM.
component := fncmp.HTML("<button>Click me</button>")
fn := fncmp.NewFn(ctx, component)
If we wanted this component in a specific location in the DOM:
fn.AppendElement("menu")
Event listeners
The ability to create and respond to DOM events is a key feature of fncmp.
We can apply a function to be called when the "click" event is triggered on the component.
component = component.WithEvents(handleClick, fncmp.OnClick)
In this example, a component 'Greeting' is returned when the "click" event is triggered.
func handleClick(ctx context.Context) fncmp.FnComponent {
return fncmp.NewFn(ctx, Greeting)
}
These short examples only hint at what can be done with fncmp. Please following the tutorial and reference documentation to learn more.