[MST] Restore the Model Tree State using Hot Module Reloading when Model Definitions Change

n this lesson, we will set up Hot Module Reloading(HMR), making it possible to load new definitions for React components and MST models and apply them to a running application.

In this lesson you will learn:

  • How HMR roughly works
  • How to accept changes to components
  • How to accept changes to changing model definitions, while keeping state
let wishList = WishList.create(initialState)

function renderApp() {
    ReactDOM.render(<App wishList={wishList} />, document.getElementById("root"))
}

renderApp()

if (module.hot) {
    module.hot.accept(["./components/App"], () => {
        // new components
        renderApp()
    })

    module.hot.accept(["./models/WishList"], () => {
        // new model definitions
        const snapshot = getSnapshot(wishList)
        wishList = WishList.create(snapshot)
        renderApp()
    })
}

 

posted @ 2018-01-28 22:55  Zhentiw  阅读(121)  评论(0编辑  收藏  举报