Created
February 8, 2017 08:47
-
-
Save nukosuke/52d9e1d8749ef754b2dd668a5d340558 to your computer and use it in GitHub Desktop.
react_on_rails, react-router, redux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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