Skip to content

Instantly share code, notes, and snippets.

@rej156
Created May 20, 2016 12:57
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rej156/4524b59ea77a697fb62a39eada5b9bbe to your computer and use it in GitHub Desktop.
Save rej156/4524b59ea77a697fb62a39eada5b9bbe to your computer and use it in GitHub Desktop.
Mobx HMR ES6 stores
import React from 'react'
import { render } from 'react-dom'
import { Router, browserHistory, match } from 'react-router'
import createStore from '../shared/lib/create-store.js'
import ContextProvider from '../shared/lib/context-provider.js'
import { fetchDataOnLocationMatch } from '../shared/lib/fetch-data.js'
import Root from './Root.jsx'
import routes from '../shared/routes.jsx'
import { AppContainer } from 'react-hot-loader'
import { observable, computed, autorun } from 'mobx'
const store = createStore(window.INITIAL_STATE)
fetchDataOnLocationMatch(browserHistory, routes, match, store)
function renderApp(Root, hmrStore) {
if (window.store) {
hmrStore = require('../shared/lib/create-store.js').default(JSON.parse(JSON.stringify(window.store)))
window.store = hmrStore
}
render(
<AppContainer>
<Root store={hmrStore}/>
</AppContainer>,
document.getElementById('root')
)
}
renderApp(Root, store)
if (module.hot) {
if (!window.store) window.store = store
module.hot.accept(() => {
renderApp(require('./Root.jsx').default)
})
}
@nschwan94
Copy link

nschwan94 commented Oct 8, 2017

any clarifications or comments on this approach would be appreciated. having trouble implementing it in a mobx / electron app. this wont preserve state on a full refresh, right? only for hmr?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment