Skip to content

Instantly share code, notes, and snippets.

@nukosuke
Created February 8, 2017 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nukosuke/52d9e1d8749ef754b2dd668a5d340558 to your computer and use it in GitHub Desktop.
Save nukosuke/52d9e1d8749ef754b2dd668a5d340558 to your computer and use it in GitHub Desktop.
react_on_rails, react-router, redux
import React from 'react';
import { Provider } from 'react-redux';
import { match, RouterContext } from 'react-router';
import ReactOnRails from 'react-on-rails';
import Helmet from 'react-helmet';
import routes from '../routes/routes';
import store from '../store/store';
// for header title server side rendeting on first load
// ref: http://r7kamura.hatenablog.com/entry/2016/10/10/173610
global.Helmet = Helmet;
const Router = (props, railsContext) => {
const store = ReactOnRails.getStore("store");
let error;
let redirectLocation;
let routeProps;
const { location } = railsContext;
match({ routes, location }, (_error, _redirectLocation, _routeProps) => {
error = _error;
redirectLocation = _redirectLocation;
routeProps = _routeProps;
});
if (error || redirectLocation) {
return { error, redirectLocation };
}
return (
<Provider store={store}>
<RouterContext {...routeProps} />
</Provider>
);
};
ReactOnRails.register({
Router,
});
ReactOnRails.registerStore({
store,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment