Lifecycle
The life of a stimulus reflex
Last updated
Was this helpful?
The life of a stimulus reflex
Last updated
Was this helpful?
StimulusReflex supports 4 lifecycle events.
before
- prior to sending a stimulate request over the web socket
success
- after the server side reflex succeeds and the DOM has been updated
error
- whenever the server side reflex raises an error
after
- after both success
and error
Using the lifecycle is not a requirement. Think of it as a power tool that will help you build more sophisticated applications.
Simply declare lifecycle methods in your StimulusReflex controller.
The methods beforeUpdate
and afterUpdate
use a naming convention that matches their suffix to the reflex method name ExampleReflex#update
StimulusReflex controllers can define 4 generic lifecycle methods which provide a simple way to hook into descendant reflexes.
beforeReflex
reflexSuccess
reflexError
afterReflex
StimulusReflex controllers can define 4 custom lifecycle methods for each descendant reflex. These methods use a naming convention based on the reflex. For example, the reflex ExampleReflex#update
will produce the following lifecycle methods.
beforeUpdate
updateSuccess
updateError
afterUpdate
It's not required to implement all lifecycle methods. Pick and choose which lifecycle methods makes sense for your application.
Lifecycle methods apply a naming convention based on the reflex. For example, the reflex ExampleReflex#do_stuff
will produce the following lifecycle methods.
beforeDoStuff
doStuffSuccess
doStuffError
afterDoStuff
Both generic and custom lifecycle methods share the same function arguments.
beforeReflex(element)
element - the element that triggered the reflex
reflexSuccess(element)
element - the element that triggered the reflex
reflexError(element, error)
element - the element that triggered the reflex error - the error message
afterReflex(element, error)
element - the element that triggered the reflex error - the error message if an error occurred, otherwise null