How to integrate React with a Rails/PHP/X backend?

23 May 2017

So you have a classical server-rendered web app in PHP or Rails or something else entirely. You’ve heard of React — maybe even played with it a little.

You want to add some React superpowers to your existing application. But…

Should I separate the frontend from the backend completely?

Is it a best practice to refactor my app so that it’s just an API, and host the React app some place else?

So, should you? or is it?

One of the beauties of React is that you absolutely don’t have to buy all-in. Instead of a big-bang rewrite, you can start introducing React where it’s the easiest, and gradually work your way through the migration. Migrate the simplest components. Consider making use of React to build a tiny new feature.

Complete rewrites are time- and money-consuming and they may prevent you from tackling new features that you already have in your backlog — and which are more relevant to your users than some rewrite.

Don’t listen to people telling you how everything has to be. Be reasonable and do what makes sense in your case.

You may use something like react-rails or a similar library for your web framework, if only as a transitional step. You may find that this will work just fine for you and you might not even need a full-blown SPA. If you do, however, going off react-rails has to be a lot easier than rewriting a sizeable codebase from scratch.

The roadmap could be as follows:

Finally, there’s an amazing talk by Ryan Florence about switching to React. The example is in Backbone, but the idea is more general and applies to server-rendered HTML or jQuery spaghetti as well!

There is another interesting post about going from PHP to React.

Think your friends would dig this article, too?

Google+
Tumblr

Want to level up your React skills?

Sign up below and I'll send you articles just like this about React straight to your inbox every week or so.

No spam, promise. I hate it as much as you do!

, enjoying the article? Now think of 3 friends who are interested in React and would be into it, and share the link with them! 👇

https://goshakkk.name/integrating-react-with-rails-php-backends/