Actions
The magic sauce. Actions allow you to easily listen to page interactions and call a method on your Wires.
Last updated
Was this helpful?
The magic sauce. Actions allow you to easily listen to page interactions and call a method on your Wires.
Last updated
Was this helpful?
Actions provide an effortless way to track page interactions and invoke methods on your , resulting in a re-render of the Wire's .
Here is a basic example of how to use it:
You can listen for browser events and invoke actions using a selection of various . The directives follow the format: wire:[browser event]=[action].
Some examples of events you can listen for include:
click
wire:click
keydown
wire:keydown
submit
wire:submit
Here are a few examples of each in HTML:
You can listen for any browser events on elements by using the wire:[event] directive, where [event] is the event's name. For example, to listen for a "foo" event on a button element, you would use the following code:
You can pass parameters to your actions, such as here using addTask('Some Task')
.
Actions shouldn't return any value. Return values are ignored.
You can access data properties inside your actions directly using data
.
You can access Computed Properties inside your actions directly using computed
.
In CBWIRE, there are some "magic" actions that are usually prefixed with a "$" symbol:
$refresh
Will re-render the component without firing any action
$set( 'dataproperty', value )
Shortcut to update the value of a property
$toggle( 'dataproperty' )
Shortcut to toggle boolean properties off and on
Consider the example below.
You can instead call $set and avoid the need to create an Action named setMessageToHello.
It can also be used in the backend when listening for an event. For example, if you have one component that emits an event like this:
Then in another component you can use a magic action for example $refresh() instead of having to point the listener to a method:
The parameter is then passed through to your via function arguments.