Advanced Use
How to get the most out of StimulusReflex
Last updated
Was this helpful?
How to get the most out of StimulusReflex
Last updated
Was this helpful?
StimulusReflex leverages Rails ActionCable. Understanding what Rails provides out of the box will help you get the most value from this library.
The ActionCable defaults of window.App
and App.cable
are used if they exist. A new socket connection will be established if these do not exist.
ActionCable emits verbose log messages. Disabling ActionCable logs may improve performance.
You might find the need to restrict communication to a specific room. This can be accomplished in 2 ways.
Passing the room name as an option to register
.
Setting the data-room
attribute on the StimulusController element.
Setting room in the body with a data attribute can pose a security risk. Consider assigning room when registering the Stimulus controller instead.
An attempt is made to reduce repaint jitter when users trigger several updates in succession.
You can control how long to wait prior to updating the DOM - think debounce. Simply set the renderDelay
option in milliseconds when registering the controller.
renderDelay
defaults to25
milliseconds.
Server side reflexes inherit from StimulusReflex::Reflex
and hold logic responsible for performing operations like writing to your backend data stores. Reflexes are not concerned with rendering because rendering is delegated to the Rails controller and action that originally rendered the page.
connection
- the ActionCable connection
channel
- the ActionCable channel
request
- an ActionDispatch::Request
proxy for the socket connection
session
- the ActionDispatch::Session
store for the current visitor
url
- the URL of the page that triggered the reflex
element
- a Hash like object that represents the HTML element that triggered the reflex
element
The element
property contains all of the Stimulus controller's DOM element attributes as well as other properties like checked
and value
.
Most values are strings. The only exceptions are checked
and selected
which are booleans.
Here's an example that outlines how you can interact with the element
property in your reflexes.