import{Controller}from'stimulus'importStimulusReflexfrom'stimulus_reflex'exportdefaultclassextendsController{connect(){StimulusReflex.register(this)}beforeUpdate(){ // show spinner}afterUpdate(){ // hide spinner}}
The methods beforeUpdate and afterUpdate use a naming convention that matches their suffix to the reflex method name ExampleReflex#update
Generic Lifecycle Methods
StimulusReflex controllers can define 4 generic lifecycle methods which provide a simple way to hook into descendant reflexes.
beforeReflex
reflexSuccess
reflexError
afterReflex
Custom Lifecycle Methods
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.
Conventions
Method Names
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
Method Signatures
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