I’m rebuilding the front end of a Rails 5 project as a single page app in React. Here’s my “quick and dirty” search and replace approach to jump-starting the process.
The dynamic content that was previously generated by Ruby/Rails will need to be manually refactored so that it is handled by your React component(s). This is where most of the work will happen. To make this more straight-forward, I converted the ERB tags to instead output strings so it was easy to see where I needed to refactor.
After this, you will see things like
@person.name in the rendered HTML. It’s ugly, but that’s the point. It needs to get replaced anyway, so might as well make it obvious what should eventually go there.
Class and for Attributes
= in the search/replace term, but if your code has spaces around the
=, you’ll need to make adjustments.
I started by copy/pasting my layout file from Rails into the public/index.html file provided by create-react-app. I replaced the Rails
<div id="root"></div>. Then I copy/pasted the entire view file into the render function in src/App.js.
The plan was to leave the legacy code in index.html as static HTML while breaking down all the dynamic content in the App component into more granular components. This is fine, but if there are ERB tags, etc in the index.html file then webpack will probably error out. I don’t know exactly what’s going on under the hood, but I had to remove all the ERB tags from index.html also before webpack could successfully compile the bundle. This isn’t hard to do, but the errors might not be very helpful or clear about what is going on. I spent a couple hours debugging the JSX in App.js before I figured out that the source of the problem was a different file.