Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Hot Reloading of model #168

Closed
Chrisigrimm opened this issue May 9, 2019 · 9 comments
Closed

Support Hot Reloading of model #168

Chrisigrimm opened this issue May 9, 2019 · 9 comments
Labels
docs Stuff that should be added to the docs
Milestone

Comments

@Chrisigrimm
Copy link

How can hot reloading be implemented?
I tried it with creating a new store and render the app new but that doesn't work.
Any ideas? Is there a way to create reducers out of the models? Then i can use the standard function replaceReducer of the store.

renderApp(App, store, reduxHistory);

function renderApp(App: any, store: any, history: any) {
	ReactDOM.render(
        <StoreProvider store={ store}>
            <Router history={ history}>
				<MuiThemeProvider theme={theme}>
					{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
					<CssBaseline />
                    <App />
				</MuiThemeProvider>
			</Router>
		</StoreProvider>,
		document.getElementById('root')
	);
}

// Connect HMR
if ((module as any).hot) {
	(module as any).hot.accept([ './model' ], () => {
		store = createStore(model, {
			devTools: true,
            middleware: [routerMiddleware],
            initialState: store.getState()
        } as EasyPeasyConfig);
	renderApp(App, store, reduxHistory);
	});
}
@Chrisigrimm Chrisigrimm changed the title How to implement React Hot Reloading How to implement Hot Reloading May 9, 2019
@ctrlplusb
Copy link
Owner

Hey @Keyj1n

Thanks for raising this. I think I already have most of the work done for this, but need to do a bit more still.

I would appreciate it if you could create a minimal example repo for me to test against. 👍

@ctrlplusb ctrlplusb changed the title How to implement Hot Reloading Support Hot Reloading of model May 9, 2019
@ctrlplusb ctrlplusb added the 🧡help wanted 🧡 Extra attention is needed label May 18, 2019
@ctrlplusb ctrlplusb mentioned this issue May 18, 2019
8 tasks
@allpro
Copy link
Contributor

allpro commented Jul 10, 2019

I am having an issue with hot reloading right now. All the data in EP models is lost, yet the rest of the store is fine. It's hard to tell if this is related to Easy-Peasy or the convoluted flow currently used. I'm also using a reducerEnhancer to integrate older reducers, which adds to the complexity.

What is the status of hot reloading with EP? Anything you can share would be helpful. One way or another I'll have to find a solution, no matter how hacky!

@ctrlplusb ctrlplusb added this to the 3.0.0 milestone Jul 10, 2019
@ctrlplusb
Copy link
Owner

ctrlplusb commented Jul 10, 2019

This works in easy-peasy@3.0.0-beta.1, via the new store.reconfigure(model) API.

I modified a simple Create React App to test it and appears to work. You can modify the state by interacting with the application then make a code change to the model. You will note that the app is updated by state is maintained. 👍

I introduced the following file structure:

|- src
   |- model
   |  |- index.js
   |
   |- index.js
   |- store.js

src/index.js

Firstly, configure the root file responsible for rendering your app to support hot reloading of the App component.

import React from "react";
import ReactDOM from "react-dom";
import { StoreProvider } from "easy-peasy";
import App from "./App";
import store from "./store";

const render = Component =>
  ReactDOM.render(
    <StoreProvider store={store}>
      <Component />
    </StoreProvider>,
    document.getElementById("root")
  );

render(App);

if (process.env.NODE_ENV === "development") {
  if (module.hot) {
    module.hot.accept("./App", () => {
      const NextApp = require("./App").default;
      render(NextApp);
    });
  }
}

src/store.js

Then configure your store to support hot reloading of the model.

import { createStore } from "easy-peasy";
import model from "./model";

const store = createStore(model);

if (process.env.NODE_ENV === "development") {
  if (module.hot) {
    module.hot.accept("./model", () => {
      store.reconfigure(model);  // 👈 Here is the magic
    });
  }
}

export default store;

@ctrlplusb
Copy link
Owner

@allpro @Keyj1n 👆

@ctrlplusb ctrlplusb added docs Stuff that should be added to the docs and removed 🧡help wanted 🧡 Extra attention is needed labels Jul 10, 2019
@ctrlplusb ctrlplusb mentioned this issue Jul 10, 2019
Merged
5 tasks
@allpro
Copy link
Contributor

allpro commented Jul 10, 2019

Thanks @ctrlplusb. Will give this a try today.

@ctrlplusb
Copy link
Owner

Created an example repo and have added to v3 docs

https://github.com/ctrlplusb/easy-peasy-hot-reload

@jamaljsr
Copy link

Tested and confirmed working for me. Thanks @ctrlplusb 👍

@lishine
Copy link

lishine commented Jul 28, 2019

Thanks for this.
Does somebody has a clue how to do this with Next.js ?

@codemonkeynorth
Copy link

Vite version

if (import.meta.hot) {
  import.meta.hot.accept("./model", (newModule) => {
    const newModel = newModule.default
    store.reconfigure(newModel) // 👈 Here is the magic
  })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Stuff that should be added to the docs
Projects
None yet
Development

No branches or pull requests

6 participants