Skip to content

Instantly share code, notes, and snippets.

@mikaelvesavuori
Created February 9, 2019 15:26
Show Gist options
  • Save mikaelvesavuori/d88fd8e68c7da90c9b071e69338b4c36 to your computer and use it in GitHub Desktop.
Save mikaelvesavuori/d88fd8e68c7da90c9b071e69338b4c36 to your computer and use it in GitHub Desktop.
SSR React base using Unistore (keeping entirely as reminder)
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