Created
February 9, 2019 15:26
-
-
Save mikaelvesavuori/d88fd8e68c7da90c9b071e69338b4c36 to your computer and use it in GitHub Desktop.
SSR React base using Unistore (keeping entirely as reminder)
This file contains hidden or 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 { StaticRouter } from 'react-router'; | |
import { createStore, Provider } from 'unistore/full/react'; | |
import Routes from 'routes/index'; | |
import persistStore from 'unissist'; | |
import localStorageAdapter from 'unissist/integrations/localStorageAdapter'; | |
// Use let rather than const so we can send props in class constructor | |
let store = createStore({}); | |
const adapter = localStorageAdapter(); | |
persistStore(store, adapter); | |
export default class ServerApp extends React.Component { | |
constructor(props) { | |
super(props); | |
store = createStore(this.props.initialState); | |
} | |
render(props) { | |
return ( | |
<StaticRouter location={store.getState().url} context={this.props.context}> | |
<Provider store={store}> | |
<Routes initialRoute={store.getState().url} /> | |
</Provider> | |
</StaticRouter> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment