Rails 7, Reactjs setup | monolith
Rails 7 comes with importmaps
as default for running and managing javascript code. AFAIK importmaps
is pretty much a new player and does not support .jsx
extensions for your react components. Due to this reason, I am going to use esbuild
, which supports both jsx
and typescript
code.
Init rails 7
Create new rails app with esbuild
a javascript bundler 🛺.
Init React 18
Install react
and react-dom
dependencies 📎,
Inertia & vite 🚧
Before creating the components, we will add an additional plugin inertia
.
In simple terms it allows us to write backend and front in a single code base, where you don't have to write or add any serializers or helpers to return JSON
responses for API
integration.
Inertia has two parts, where one is an adapter, which will handle the incoming request and responses from client-side javascript code supported by client-side inertia SDK. First, install client-side SDK by issuing the following command,
Install inertia_rails
gem to your rails app,
The inertia
client-side SDK uses ES6 dynamic imports, this feature is not yet fully supported by esbuild
. So we will use vite
for react
. vite_rails
provides similar functionality as webpack
.
The last step adds bin/vite
and vite
configuration files and entrypoint
files in javascript
. and make some changes to rails default layout application.html.erb
. Verify the vite
installation using the following command,
Add some business logic 🚦
This current application needs to greet some random user, whenever someone visits the web page. Let's start adding the missing pieces (react components and rails controllers),
Your client-side code
- Create
components
directory to hold the view layer logic, (name it whatever you want)
- Create a
Hello
component with the following code. The below component holds some of our business logic, i.e, accepts a prop calledname
and render it.
- Create a
Application
file,
- Add the following code to
Application
file, which will create the required bindings and mount the app into your rails application view file*.html.erb
- Register your react application in
entrypoints/application.js
file ,
Your server-side code
- Install
faker
to randomly generate user names, and createhome
controller withindex
action.
- In
index
action, create theinertia
response, by providing the component nameHello
and provide the user name as an attribute to the page component.
- Add
root_path
tohome#index
, so that whenever we hit the/
path, the request will be redirected toHome
controller.
- Now, launch the rails server and frontend powered by
vite
🚀,
Open your browser and hit localhost:3000,
for every hit you will see a different user name 👏.
That's it, you have successfully coupled react components into your rails server, and now you can use react components in places of view.
Final thoughts 🥸
- This approach will be well suited for monoliths, where the developer team size is small and does not have time to create REST APIs.
- You still use the
sessions
built-into rails. There are some security concerns though. - Quick and easy.
The source code can be found here.