GX4013: optimistic directive has no action or writes no signal
An optimistic:<event> directive is on an element with no on:<event> handler that invokes an action, or its statements write no signal of the component.
Cause
An optimistic:<event> directive changes signals before the request of an action. When the action fails, the runtime puts the values of before back.
The directive needs two things:
- An
on:<event>handler on the same element, for the same event, that invokes an action. Without it, there is no request that can fail. - A statement that writes a signal of the component. The runtime saves only the signals that the statements write.
Example
package route
import "github.com/alternayte/gx"
type Like struct {
gx.Route `POST /likes/like`
}package likes
import (
"github.com/alternayte/gx"
"app/likes/route"
)
var like = gx.Action(func(c *gx.Ctx, in route.Like) error {
return nil
})
var Routes = gx.Collect(like)package likes
signals {
Count int = 0
}
<div>
<span text={$Count}></span>
<button optimistic:click={$Count++}>Like</button>
</div>gx check reports:
likes/Likes.gx:9:11: GX4013: optimistic directive: the element has no on:click handler that invokes an actionFix
- Add the action to the element:
<button on:click={route.Like{}} optimistic:click={$Count++}>. - Use the same event in the two directives.
- For a change with no request, use
on:click={$Count++}.