Created
August 5, 2016 15:15
-
-
Save damonYuan/ca780f1b36a6a783c187391e6285ddbf to your computer and use it in GitHub Desktop.
react server side rendering without redux (client side)
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 { render } from 'react-dom'; | |
import { Provider } from 'react-redux'; | |
import { Router, browserHistory, useRouterHistory } from 'react-router'; | |
import { syncHistoryWithStore } from 'react-router-redux'; | |
import { configureStore } from './app/SpreeReactStore'; | |
import routes from './app/routes'; | |
let state = window.__initialState__ || undefined; | |
const store = configureStore(browserHistory, state); | |
const history = syncHistoryWithStore(browserHistory, store); | |
let handleCreateElement = (Component, props) => { | |
if(Component.hasOwnProperty('requestInitialData') && document.getElementById('initial-data')) { | |
let initialData = document.getElementById('initial-data').textContent; | |
if(initialData.length > 0) { | |
initialData = JSON.parse(initialData); | |
} | |
return <Component initialData={initialData} {...props} />; | |
} else { | |
return <Component {...props} />; | |
} | |
}; | |
render( | |
<Provider store={store}> | |
<Router history={history} createElement={handleCreateElement} routes={routes}/> | |
</Provider>, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment