Introducing a new way to review your PRs.

Read the announcement

shakacode / react-webpack-rails-tutorial

Legacy full-app React on Rails tutorial repo and live demo. See reactonrails.com/examples for current maintained demos.

View on GitHub

16 documents · Updated Nov 4, 2026

docs / rescript-comment-demo

ReScript comment demo

The ReScript comment demo is a React page that shows how a typed ReScript component can use the Rails comment service. It lets a user choose a form layout, submit a comment, render Markdown, and receive comment updates through Action Cable. The page is available at /rescript and is one of several comment interfaces under the comment experience.

Page role

The page connects a Rails view to a ReScript React component. Rails supplies the page route and React on Rails supplies the component boundary. The browser then manages form state, comment loading, and live updates inside the component.

Page entry point

The Rails route sends /rescript to the page action. The view renders the RescriptShow component with server prerendering enabled, so the ReScript page participates in the same Rails-to-React delivery model as the other demos. The comment data is loaded by the component rather than embedded in the page action. app/views/pages/rescript.html.erb config/routes.rb app/…/pages_controller.rb

References: app/…/rescript.html.erb• L1 config/routes.rb• L11-16 app/…/pages_controller.rb• L38-40

Component boundary

RescriptShow owns the page-level state. It starts the initial comment request and opens a subscription to the comments channel. It passes the current comments to the list and passes a refresh action to the form. This keeps the form and list focused on display while the page coordinates their shared data.

References: client/…/ReScriptShow.res• L1-17 client/…/ReScriptShow.res• L22-94

Comment interaction model

The interaction model separates user input from comment display. The form reports a successful save to the page, and the page refreshes the list. A channel update can also replace the current list without a page reload.

Form variants

The form offers horizontal, inline, and stacked layouts without changing the comment contract. A single local selection controls which layout is shown, while all variants use the same author and text values, change handlers, submit handler, and save state. The initial layout is horizontal.

References: client/…/CommentForm.res• L1-20 client/…/CommentForm.res• L42-60 client/…/CommentForm.res• L92-142 client/…/HorizontalForm.res• L1-50 client/…/InlineForm.res• L1-45 client/…/StackedForm.res• L1-45

Comment submission

Submission is an asynchronous flow with three visible states: idle, saving, and error. The form prevents the browser's normal submit, sends the author and text to the Rails JSON endpoint, disables the form while the request is active, and refreshes the list after a successful save. A failed save keeps the user on the page and shows an error.

The end-to-end submission and refresh path is shown below.

sequenceDiagram
    actor User
    participant Form as ReScript form
    participant Page as ReScript page state
    participant Rails as Rails comments endpoint
    participant List as Comment list

    User->>Form: Enter author and text
    User->>Form: Submit
    Form->>Page: Set saving state
    Form->>Rails: POST comment as JSON
    alt Save succeeds
        Rails-->>Form: Success
        Form->>Page: Fetch current comments
        Page->>Rails: GET comments as JSON
        Rails-->>Page: Comment collection
        Page->>List: Render collection
    else Save fails
        Rails-->>Form: Validation or request error
        Form->>Page: Set error state
    end

References: client/…/CommentForm.res• L23-90 client/…/Actions.res• L2-24 client/…/ReScriptShow.res• L31-37

Comment list

The list turns the decoded comment collection into stable comment entries. Each entry shows the author and converts the text from GitHub-flavored Markdown to HTML. New entries and error messages use the same fade transition so changes remain visible without a full page redraw.

References: client/…/CommentList.res• L3-23 client/…/Comment.res• L1-19 client/…/AlertError.res• L3-25 client/…/ReScriptShow.module.scss• L1-20

Live comment updates

The page subscribes to CommentsChannel when it mounts and removes that subscription when it unmounts. Each received comment becomes the page's current comment data, which lets a second browser see a new comment without submitting its own form. The broader delivery path is documented in Realtime comment updates.

References: client/…/ReScriptShow.res• L18-20 client/…/ReScriptShow.res• L39-64 client/…/ActionCable.res• L1-39 app/…/comments_channel.rb• L1-7

JavaScript and Rails boundaries

ReScript does not implement the network and browser libraries itself. It defines small typed boundaries for the JavaScript libraries and uses those boundaries to keep the page's data shape clear as it crosses into Rails and the browser.

Action bindings

The action layer defines two comment contracts. The create contract carries an author and text to comments.json. The fetch contract expects a response with a comments array whose entries contain an integer ID, an author, and text. JSON that does not match this shape becomes a fetch error, which the page displays instead of rendering partial data.

References: client/…/Actions.res• L2-24 client/…/Actions.res• L27-59 app/…/comments_controller.rb• L23-45

JavaScript library interop

JavaScript interop means a ReScript declaration gives a typed name to a JavaScript library. This demo uses that boundary for Axios requests, Fetch and JSON decoding, Markdown conversion, Action Cable subscriptions, and transition components. The bindings keep library calls in one place while the page and its child components work with ReScript values.

References: client/…/Axios.res• L1 client/…/Marked.res• L1-2 client/…/ActionCable.res• L1-39 client/…/ReactTransitionGroup.res• L1-26 client/…/Actions.res• L38-57

Generated component bridge

React on Rails discovers components from its ror_components directory. The small JavaScript bridge exports the compiled ReScript component under the name RescriptShow, so the Rails view can request that name without knowing the ReScript source path. The bridge is therefore the handoff between generated ReScript output and React on Rails registration.

References: client/…/RescriptShow.jsx• L1-8 config/…/react_on_rails.rb• L4-8 app/…/rescript.html.erb• L1

Build lifecycle

The ReScript page has a two-stage build lifecycle. ReScript first turns .res sources into in-source ES modules. Shakapacker then resolves those generated .res.js modules and includes them in the browser and server bundles.

ReScript compilation

The ReScript project is limited to the comment demo directory and its subdirectories. Its package scripts provide clean, watch, format, and full-build steps. The project uses Node 22 or later with Yarn 1.22, and the package manager is pinned to Yarn 1.22.22. It emits ES modules with the .res.js suffix, and its dependencies provide React, Fetch, JSON decoding, and the React on Rails bridge. A clean build removes old generated output before compilation so stale modules do not hide source changes.

References: bsconfig.json• L1-27 package.json• L5-7 package.json• L19-25 package.json• L181

Asset bundling

Shakapacker resolves .res.js as a module type alongside the normal JavaScript and TypeScript types. Build commands compile ReScript before they generate React on Rails pack metadata and run Shakapacker. This order makes the generated component bridge and its imported ReScript module available to the pack build.

References: config/…/commonWebpackConfig.js• L8-23 config/…/commonWebpackConfig.js• L39-51 config/…/react_on_rails.rb• L9-17 package.json• L31-32

Development and release builds

Development uses a ReScript watcher so generated modules change as the source changes. Test and production builds use a clean ReScript build before pack generation. The development process layout also runs the Rails server and the client asset watcher, while the release path produces the assets that Rails serves. The test:client script runs the client test suite without rebuilding the full Rails asset set.

References: README.md• L328-340 config/shakapacker.yml• L26-37 config/shakapacker.yml• L64-75 package.json• L21-25 package.json• L29-34 config/…/react_on_rails.rb• L9-17

The ReScript page shares the Rails comment service with the other comment demos but presents a separate client model. Use the related pages to compare its boundaries with the other approaches:

References: client/…/ReScriptShow.res• L31-94 client/…/Actions.res• L2-59 app/…/comments_controller.rb• L23-45 app/…/comments_channel.rb• L1-7

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the Rails component name, the bridge export, and the view's react_component name aligned when the page entry point changes.
  • Keep the create payload and fetch decoder aligned with the Rails JSON response when comment fields change.
  • Keep the Action Cable payload shape aligned with the received callback when live event data changes.
  • Keep the transition timeout and the SCSS transition duration aligned when animation timing changes.
  • Keep the ReScript build step before pack generation in test, development, and production workflows when build commands change.
  • Keep the required Node and Yarn versions aligned with the package scripts and the local development process when the JavaScript toolchain changes.

Important failure modes

TriggerConsequenceGuard
The generated ReScript module is missing or stale.The bridge cannot load the current component or the page runs old code.Run the clean ReScript build before the pack build.
The view name, bridge export, and registered component name differ.React on Rails cannot mount RescriptShow.Treat those names as one component contract.
The comments JSON shape changes without a decoder update.The initial load fails and the page shows “Can't fetch the comments!”.Update the fetch contract with the Rails response.
Author or text validation fails during save.No comment is added and the form shows “Can't save the comment!”.Preserve the required fields and the error path.
The channel event no longer matches the received comment shape.Live updates cannot become valid list entries.Keep the relay payload and Actions.Fetch.t contract aligned.
The CSS transition duration and component timeout differ.Comments or errors can appear or leave with a broken fade.Update both timing values together.
The local Node or Yarn version does not meet the package contract.ReScript or asset commands can fail before the page build starts.Use Node 22 or later and the pinned Yarn 1.22 toolchain.

Key files

FileWhy to read it
app/views/pages/rescript.html.erbShows the Rails entry point and prerender boundary.
client/app/bundles/comments/rescript/ReScriptShow.resShows page state, initial loading, channel subscription, and component composition.
client/app/bundles/comments/rescript/CommentForm/CommentForm.resShows form selection, save state, and submit coordination.
client/app/bundles/comments/rescript/CommentForm/forms/HorizontalForm.resShows the shared input and submit interface for one form layout.
client/app/bundles/comments/rescript/CommentList/CommentList.resShows collection-to-component list rendering.
client/app/bundles/comments/rescript/CommentList/Comment/Comment.resShows Markdown display and comment transition behavior.
client/app/bundles/comments/rescript/Actions/Actions.resDefines the Rails request and response contracts.
client/app/bundles/comments/rescript/bindings/ActionCable.resDefines the browser channel boundary used for live updates.
client/…/RescriptShow.jsxBridges compiled ReScript output to React on Rails discovery.
bsconfig.jsonDefines ReScript source scope and generated module format.
config/…/commonWebpackConfig.jsMakes generated ReScript modules available to asset bundling.
config/…/react_on_rails.rbDefines component discovery and build ordering at the Rails boundary.
package.jsonDefines the ReScript compilation, client test, and asset build commands plus the Node and Yarn toolchain contract.
spec/…/rescript_spec.rbDescribes the supported form switching and submission behavior.