Lifecycle
The life of a stimulus reflex
StimulusReflex supports 4 lifecycle events.
before
- prior to sending a stimulate request over the web socketsuccess
- after the server side reflex succeeds and the DOM has been updatederror
- whenever the server side reflex raises an errorafter
- after bothsuccess
anderror
Quick Start
Simply declare lifecycle methods in your StimulusReflex controller.
<a href="#"
data-controller="example"
data-reflex="click->ExampleReflex#update">
import { Controller } from 'stimulus'
import StimulusReflex from 'stimulus_reflex'
export default class extends Controller {
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
<div data-controller="example">
<a href="#" data-reflex="ExampleReflex#update">Update</a>
<a href="#" data-reflex="ExampleReflex#delete">Delete</a>
</div>
import { Controller } from 'stimulus'
import StimulusReflex from 'stimulus_reflex'
export default class extends Controller {
connect () {
StimulusReflex.register(this)
}
beforeReflex(anchorElement) {
const { reflex } = anchorElement.dataset
if (reflex.match(/update$/)) anchorElement.innerText = 'Updating...'
if (reflex.match(/delete$/)) anchorElement.innerText = 'Deleting...'
}
}
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
<div data-controller="example">
<a href="#" data-reflex="ExampleReflex#update">Update</a>
<a href="#" data-reflex="ExampleReflex#delete">Delete</a>
</div>
import { Controller } from 'stimulus'
import StimulusReflex from 'stimulus_reflex'
export default class extends Controller {
connect () {
StimulusReflex.register(this)
}
beforeUpdate(anchorElement) {
anchorElement.innerText = 'Updating...'
}
beforeDelete(anchorElement) {
anchorElement.innerText = 'Deleting...'
}
}
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 reflexreflexSuccess(element)
element - the element that triggered the reflexreflexError(element, error)
element - the element that triggered the reflex error - the error messageafterReflex(element, error)
element - the element that triggered the reflex error - the error message if an error occurred, otherwisenull
Last updated
Was this helpful?