StimulusReflex

Build reactive applications with the Rails tooling you already know and love.

GitHub starsarrow-up-right GitHub forksarrow-up-right Twitter followarrow-up-right

Build reactive applications with the Rails tooling you already know and love. StimulusReflex is designed to work perfectly with server rendered HTMLarrow-up-right, Russian doll cachingarrow-up-right, Stimulusarrow-up-right, Turbolinksarrow-up-right, etc... and strives to live up to the vision outlined in The Rails Doctrinearrow-up-right.

Ship projects faster... with smaller teams.

Before you Begin

A great user experience can be created with Rails alone. Tools like UJS remote elementsarrow-up-right , Stimulusarrow-up-right, and Turbolinksarrow-up-right are incredibly powerful when combined. Try building your application using these tools before introducing StimulusReflex.

circle-info

See the Stimulus TodoMVCarrow-up-right example application if you are unsure how to do this.

Benefits

StimulusReflex offers 3 primary benefits over the traditional Rails request/response cycle.

  1. All communication happens via web socket - avoids the overhead of traditional HTTP connections

  2. The controller action is invoked directly - skips framework overhead like the middleware chain

  3. DOM diffing is used to update the page - provides faster rendering and less jitter

Setup

yarn add stimulus_reflex
app/javascript/controllers/index.js
import { Application } from 'stimulus'
import { definitionsFromContext } from 'stimulus/webpack-helpers'
import StimulusReflex from 'stimulus_reflex'

const application = Application.start()
const context = require.context('controllers', true, /_controller\.js$/)
application.load(definitionsFromContext(context))
StimulusReflex.initialize(application)

Quick Start

Here are a few small contrived examples to get you started.

No JavaScript

It's possible to build a reactive application without writing any JavaScript. This requires 2 steps.

  1. Declare the appropriate data attributes in HTML.

  2. Create a server side reflex object with Ruby.

This example will automatically update the page with the latest count whenever the anchor is clicked.

circle-check
triangle-exclamation

Some JavaScript

Real world applications typically warrant setting up finer grained control. This requires 3 steps.

  1. Declare the appropriate data attributes in HTML.

  2. Create a client side StimulusReflex controller with JavaScript.

  3. Create a server side reflex object with Ruby.

This example will automatically update the page with the latest count whenever the anchor is clicked.

How it Works

Here's what happens whenever a StimulusReflex::Reflex is invoked.

  1. The page that triggered the reflex is re-rerendered.

  2. The re-rendered HTML is sent to the client over the ActionCable socket.

  3. The page is updated via fast DOM diffing courtesy of morphdom.

circle-check
circle-info

The entire body is re-rendered and sent over the socket. Smaller scoped DOM updates may come in a future release.

Example Applications

Last updated