General Troubleshooting and Getting Started with React on Rails

react on railsNovember 07, 2016Dotby Justin Gordon

Recently, I’ve been doing more private coaching/pairing sessions on getting setup with React on Rails. In this article, I’m going to share my general tips for maximum productivity when using React on Rails. Many of these tips will apply regardless of your programming environment.

General Tips

  1. Do the simplest possible thing. Do not try to make all the advanced features of React on Rails, like server rendering, work at first!
  2. Run code that you know works. The first is just to download some code, compile it, run it, etc. To check if your machine is setup for React on Rails, git clone the React Webpack Rails Tutorial and run it!
  3. Start off with something that works. Then take baby steps! Add one small component. Get that one thing working first, then move on to something else.
  4. Read the error messages very carefully, especially the very first error message.
  5. Figure out the first place that something is going wrong. Ignore issues that are happening much later than the first error.
  6. Only run the part of the system that is causing the error. For example, if you’re having an error in the webpack compilation, run that command by itself until you resolve any errors.
  7. Be very careful that your installed version of software matches the documentation you are reading. Often, Github will display the latest documentation on the master branch for unreleased software. You’re probably not using unreleased software. You might switch the displayed branch to match up with what you are using.
  8. Check for errors in all the places where you’ll have logs. The main places to check are: 1) Output from running foreman on the console. 2) The console in the Chrome debugger. 3) Your log files for Rails.

React on Rails Specifics

  1. Be sure to understand simpler examples before you tackle more complicated examples. Study the output of running the React on Rails generator on a new Rails app. See the videos: React On Rails Tutorial Series. When you’ve got the basics, then see a live example at reactrails.com and its source code on github.
  2. Skip server rendering. Ensure that client rendering works perfectly before turning on server rendering.
  3. Skip hot reloading. Hot reloading allows you to see the browser update after you hit save in the editor. The alternative is to press CMD-R to refresh your browser screen. Get regular “static” loading working first. When you’re ready for hot reloading, check out the docs.
  4. Skip CSS Modules and creating CSS from Webpack, initially. You can do all your CSS the way you’ve always done it with Rails. When you’re ready, you’ll definitely want to consider using CSS Modules. The React Webpack Rails Tutorial uses them! Once you use them, you won’t go back!
  5. Understand the flow of what happens when you start your Procfile in Rails. Open up the Procfile and see what’s in it. The key thing to note is that the Webpack command must successfully complete before you want to look at the Rails part. The webpack file will create one or more files for inclusion by your Rails app. These files are typically included in a Rails manifest file for your JavaScript. That file will be loaded by your main layout. When there’s a component on a view from the react_component, the React on Rails JavaScript will detect that and bootstrap the rendering of your component.
  6. Start off with very simple React code. Then add popular libraries, like redux, react-router, immutable.js, only when you understand why you need them. Don’t be shy about putting in a generous amount of console.log statements in your JavaScript code to see what’s happening.

If your time is valuable, consider signing up for a ShakaCode's React on Rails Pro. For $135 per month or $1620 per year, you get a private Slack channel, code reviews, pairing sessions, and the support of the whole ShakaCode team. For more information, click here or email me at justin@shakacode.com.

Here are some of my favorite debugging articles that apply to React on Rails. You can find more by searching for debugging at forum.shakacode.com.

image2

image1

Closing Remark

Could your team use some help with topics like this and others covered by ShakaCode's blog and open source? We specialize in optimizing Rails applications, especially those with advanced JavaScript frontends, like React. We can also help you optimize your CI processes with lower costs and faster, more reliable tests. Scraping web data and lowering infrastructure costs are two other areas of specialization. Feel free to reach out to ShakaCode's CEO, Justin Gordon, at justin@shakacode.com or schedule an appointment to discuss how ShakaCode can help your project!
Are you looking for a software development partner who can
develop modern, high-performance web apps and sites?
See what we've doneArrow right