<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Himalaya Pal</title>
    <description>The latest articles on DEV Community by Himalaya Pal (@palhimalaya).</description>
    <link>https://dev.to/palhimalaya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F527290%2F9ef2cf11-11a3-43e5-a845-a28189155347.jpeg</url>
      <title>DEV Community: Himalaya Pal</title>
      <link>https://dev.to/palhimalaya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/palhimalaya"/>
    <language>en</language>
    <item>
      <title>Supercharge Your Rails Development: A Deep Dive into Vite-Rails and React Integration</title>
      <dc:creator>Himalaya Pal</dc:creator>
      <pubDate>Tue, 27 Aug 2024 02:54:03 +0000</pubDate>
      <link>https://dev.to/palhimalaya/supercharge-your-rails-development-a-deep-dive-into-vite-rails-and-react-integration-l9d</link>
      <guid>https://dev.to/palhimalaya/supercharge-your-rails-development-a-deep-dive-into-vite-rails-and-react-integration-l9d</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;For web developers navigating the expansive Ruby on Rails landscape, the pursuit of tools that elevate efficiency is constant. Enter Vite-Rails, a cutting-edge JavaScript bundler seamlessly woven into Rails, promising unparalleled development speed. In this all-encompassing guide, we'll not only walk you through the effortless installation of Vite-Rails but also empower you to embark on a compelling journey with this dynamic duo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Setting the Stage with Vite-Rails
&lt;/h2&gt;

&lt;p&gt;Before delving into the world of Vite-Rails, ensure your local machine hosts a robust Ruby on Rails application. If not, follow the official Rails installation guide. Once your Rails application is up and running, initiate the Vite-Rails integration with the following commands in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;1. bundle add vite-rails
2. bundle &lt;span class="nb"&gt;exec &lt;/span&gt;vite &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Leveraging the power of the RubyGems package manager, these commands seamlessly download and install the Vite-Rails gem along with its dependencies. Post-installation, a &lt;code&gt;vite.config.ts&lt;/code&gt; file emerges in your Rails application's root directory, serving as the nucleus for configuring Vite-Rails to suit your project's intricacies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Tailoring Vite-Rails for Your Project
&lt;/h2&gt;

&lt;p&gt;With Vite-Rails now seamlessly integrated, the spotlight turns to customization. Open the &lt;code&gt;vite.config.ts&lt;/code&gt; file in your preferred code editor and explore the multitude of options available for tailoring Vite-Rails to your project's unique requirements. A glimpse of a sample configuration is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;vite&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;RubyPlugin&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite-plugin-ruby&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nc"&gt;RubyPlugin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A pivotal configuration to note is the &lt;code&gt;entryPoint&lt;/code&gt; option, specifying the entry point for your JavaScript files. While Vite-Rails defaults to &lt;code&gt;app/frontend/index.js&lt;/code&gt;, adapt this option if your entry point resides elsewhere, like &lt;code&gt;app/javascript/entrypoints/application.js&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Infusing React into the Mix
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pre-Integration Checklist:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Establish a root route in &lt;code&gt;routes.rb&lt;/code&gt;: &lt;code&gt;root 'homepage#index'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Formulate a &lt;code&gt;HomepageController&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomepageController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Craft an &lt;code&gt;index.html.erb&lt;/code&gt; under &lt;code&gt;views/homepage&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Adapt &lt;code&gt;application.html.erb&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;%=&lt;/span&gt; &lt;span class="na"&gt;vite_javascript_tag&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="na"&gt;index.jsx&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Now, Integrate and Configure React:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Install &lt;code&gt;react&lt;/code&gt; and &lt;code&gt;react-dom&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add react react-dom
&lt;span class="c"&gt;# or&lt;/span&gt;
npm &lt;span class="nb"&gt;install &lt;/span&gt;react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Forge a &lt;code&gt;index.jsx&lt;/code&gt; in your entry point directory (e.g., &lt;code&gt;app/javascript/entrypoints/&lt;/code&gt;):
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import App from '../src/App'
import { createRoot } from "react-dom/client";

const rootElement = document.getElementById('root')
const root = createRoot(rootElement)

root.render(
  &amp;lt;App /&amp;gt;
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Architect your React code:&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Establish a `src` folder within `app/javascript/`.
- Create `App.jsx` within `app/javascript/src/`:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;```
import React from 'react'

const App = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Hello&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default App
```
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Structure your components within the `src` folder.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;Update &lt;code&gt;package.json&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"bin/vite dev"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adjust &lt;code&gt;Procfile.dev&lt;/code&gt; and execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, execute two separate commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rails s 
&lt;span class="c"&gt;# and in another terminal&lt;/span&gt;
npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Vite-Rails and React converge to create a powerhouse duo, revolutionizing Ruby on Rails development. With Vite-Rails' swift development capabilities, featuring HMR and pre-bundling, combined with React's declarative prowess simplifying intricate UIs, your development workflow will ascend to unprecedented heights.&lt;/p&gt;

&lt;p&gt;Embrace this formidable synergy to optimize performance, elevate efficiency, and seamlessly join a community of developers already propelling their Rails projects forward with Vite-Rails and React. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
