React comments user interface
The React comments user interface is a browser-facing comment experience that lets a user read and post comments while keeping the view in sync with the Rails service. It combines a Redux state flow with a form, a comment list, Markdown-safe display, language selection, and optional React Router navigation.
User interface
The comment screen joins the input and display sides of the experience. client/…/CommentScreen.jsx places CommentBox in the page and can show a short notice after a route redirect.
Comment workspace
The CommentBox is the main workspace for the React demo. It loads the current comments when it mounts, opens a live Action Cable subscription, and exposes a manual refresh action. The same workspace shows request status, save errors, language controls, a new-comment form, and the current comment stream.
The screen supports three form layouts without changing the comment contract. The layout choice only changes how the author and text fields appear. A successful post clears the text field and keeps the author value for the next post. A failed post keeps the entered values and shows the server validation response.
References: client/…/CommentScreen.jsx• L8-47 client/…/CommentBox.jsx• L33-46 client/…/CommentBox.jsx• L82-110 client/…/CommentForm.jsx• L22-111
Comment presentation
The list presents comments in newest-first order and animates changes as items enter or leave the stream. Each comment displays its author and renders its text as GitHub-flavored Markdown. The generated HTML passes through an HTML sanitizer before the browser displays it, which limits the risk from comment content.
The list also gives the user a clear fetch failure state. This keeps a failed read distinct from an empty comment stream and leaves the manual refresh action available through the parent workspace.
References: client/…/CommentList.jsx• L10-15 client/…/CommentList.jsx• L26-49 client/…/CommentList.jsx• L52-85 client/…/Comment.jsx• L12-42
Language selection
The UI selects a locale from the Redux state and supplies its messages through IntlProvider. The selector offers English, German, Japanese, Simplified Chinese, and Traditional Chinese. The choice changes labels and status text in the React comment workspace.
References: client/…/RouterCommentsContainer.jsx• L21-32 client/…/NonRouterCommentsContainer.jsx• L22-37 client/…/selectLanguage.jsx• L5-17
Redux state flow
Redux is the shared state layer for the React comments experience. It holds the ordered comment list, read and save status, request errors, and the selected locale. Containers select this state and bind the action creators before they pass the resulting view data to presentation components.
Comment state
The comment reducer treats server reads, posts, and live messages as one state flow. A successful read replaces the list. A successful post or live message adds a comment at the front only when its identifier is not already present. This de-duplication lets the post response and the broadcast for the same comment arrive without creating two visible entries.
The state separates isFetching from isSaving. It also keeps fetch errors separate from submit errors so the list can report a read problem while the form reports a save problem. Locale changes update the same state object but do not alter the comment data.
References: client/…/commentsReducer.js• L8-15 client/…/commentsReducer.js• L17-45 client/…/commentsReducer.js• L47-87 client/…/commentsConstants.js• L1-11
Comment actions
The action layer turns user intent and browser events into state changes. Read and submit actions are asynchronous thunks. Each marks its operation as active, calls the request manager, and then dispatches either a success action with server data or a failure action with the request error.
Live messages use the same reducer path as other new comments. Locale selection uses a direct action because it needs no server request.
References: client/…/commentsActionCreators.js• L4-49 client/…/commentsActionCreators.js• L51-75
Store variants
The bundle creates two closely related stores for two page modes. Both seed the comment state from server-provided props and use thunk middleware for asynchronous actions. The router-enabled store adds route state so navigation and comment state can share one Redux provider.
| Store | Role |
|---|---|
commentsStore | Supports the React comment screen without React Router. |
routerCommentsStore | Supports the React Router shell and also tracks routing state. |
References: client/…/commentsStore.js• L8-22 client/…/routerCommentsStore.js• L11-31 client/…/index.js• L1-12
Browser request handling
The request manager is the browser boundary for comment data. It reads comments with a JSON GET request to comments.json and posts a new comment to the same endpoint. The post includes the authenticity headers supplied by React on Rails so the Rails endpoint can accept the browser request.
The action creators own the request lifecycle rather than the visual components. This keeps the form focused on user input and lets the reducer provide one consistent status and error model for initial loads, manual refreshes, and submissions.
References: client/…/requestsManager.js• L1-35 client/…/commentsActionCreators.js• L51-69
Live comment updates
The browser subscribes to the CommentsChannel after the comment workspace mounts. A received comment is converted to the bundle's immutable data shape and dispatched as a message action. The reducer places a new identifier at the front of the list and ignores a duplicate identifier.
The subscription is removed when the workspace unmounts. This limits the connection to the lifetime of the visible React comment screen and avoids leaving a stale listener behind during navigation.
The flow from a browser action to a visible comment follows this path:
flowchart LR
A[Comment workspace mounts] --> B[GET comments.json]
B --> C[Redux comment state]
C --> D[Comment list]
E[User submits comment] --> F[POST comments.json]
F --> C
G[CommentsChannel message] --> C
C --> D
References: client/…/CommentBox.jsx• L56-91 client/…/commentsActionCreators.js• L30-35 client/…/commentsReducer.js• L36-45
React Router integration
The router mode supplies the comment screen inside a shared routed layout. The server uses the current Rails location to render the matching route. The browser then uses BrowserRouter to continue navigation without replacing the page shell.
The root route displays comments. The other demo routes show a routed screen and a redirect back to the root. When the redirect reaches the comment screen, the container passes route state to CommentScreen, which displays and then clears the one-time redirect notice.
The Rails route table sends the React Router paths to the page that mounts RouterApp. This gives the server a request entry point while the client-side route table decides which content appears inside the layout.
References: client/…/routes.jsx• L1-16 client/…/RouterApp.server.jsx• L10-37 client/…/RouterApp.client.jsx• L10-18 client/…/TestReactRouterRedirect.jsx• L4-17 client/…/CommentScreen.jsx• L15-27 config/routes.rb• L11-27
React on Rails entry points
React on Rails registers the two stores and the React entry points that can render the comments experience. The no-router entry point reads commentsStore and renders NonRouterCommentsContainer. The router entry points read routerCommentsStore and wrap the route tree in the matching server or browser router.
The navigation bar can use the store already associated with the current page. On the router and no-router pages it reads the comment count from Redux. On other demo pages it renders without that store, so the shared navigation does not require the comment state everywhere.
References: client/…/serverRegistration.jsx• L2-21 client/…/App.jsx• L13-21 client/…/NavigationBarApp.jsx• L19-44 client/…/RouterCommentsContainer.jsx• L16-42 client/…/NonRouterCommentsContainer.jsx• L17-43 client/…/NavigationBarContainer.jsx• L9-30 client/…/NavigationBar.jsx• L20-127
Related comment systems
This page describes the React client view within the wider comment experience. Read the comment experience page for the product-level view, then use these pages to compare the browser UI with its other delivery paths:
- Rails comment backend for the server endpoint and comment data contract.
- Realtime comment updates for the Action Cable message path.
- Turbo and Stimulus comments for the HTML-first browser experience.
- ReScript comment demo for the alternative typed client example.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the JSON field names aligned across the browser request actions, the reducer's identifier check, and the Rails comment response.
- Keep
commentsStoreandrouterCommentsStorealigned when the shared comment state or middleware contract changes. - Keep the server and browser router entry points aligned so the first render and later navigation select the same route.
- Keep Action Cable message handling aligned with post success handling so one comment identifier remains visible only once.
- Keep the locale values offered by
SelectLanguagealigned with the translation message map and theIntlProvidervalues. - Keep the submit error shape aligned with the form's field-level error display.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| comments.json returns fields that do not include the expected comment identifier, author, or text | The list cannot reliably de-duplicate or render the comment. | Preserve the response contract used by the reducer and Comment. |
| A submit request fails while the form clears its local values | The user can lose the text that explains the failed request. | CommentForm resets only when the action completes without a submit error. |
| A live message arrives after its post response | The same comment can appear twice if identifier matching changes. | The reducer checks existing identifiers before adding a comment. |
| The Action Cable subscription is not closed when the screen unmounts | A hidden screen can keep receiving messages and hold a stale connection. | Unsubscribe and disconnect in the component unmount path. |
| Server and browser router entry points use different route behavior | The first rendered page can differ from the hydrated client view. | Keep the shared route tree and location handling consistent. |
| Markdown output bypasses sanitization | Comment text can inject unsafe HTML into the page. | Sanitize generated markup before inserting it into the DOM. |
Key files
| File | Why to read it |
|---|---|
| client/…/CommentBox.jsx | Main workspace that joins loading, form submission, live updates, locale selection, and presentation. |
| client/…/CommentForm.jsx | User input flow, form layout choices, submit status, and validation display. |
| client/…/CommentList.jsx | Comment stream presentation and fetch failure state. |
| client/…/Comment.jsx | Markdown display and HTML safety boundary. |
| client/…/commentsActionCreators.js | Browser intent and request results mapped to Redux actions. |
| client/…/commentsReducer.js | Single state model for reads, posts, live messages, errors, status, and locale. |
| client/…/requestsManager.js | Browser-to-Rails request boundary for comment reads and posts. |
| client/…/commentsStore.js | Store setup for the no-router React page. |
| client/…/routerCommentsStore.js | Store setup that adds route state to the comment flow. |
| client/…/routes.jsx | Route tree shared by server and browser rendering. |
| client/…/RouterApp.server.jsx | Server-side router entry point for the routed React page. |
| client/…/RouterApp.client.jsx | Browser-side router entry point for hydration and navigation. |
| client/…/App.jsx | No-router React entry point and Redux provider. |