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

Does react-router v4 support Hot Module Replacement #4572

Closed
helfi92 opened this issue Feb 21, 2017 · 6 comments
Closed

Does react-router v4 support Hot Module Replacement #4572

helfi92 opened this issue Feb 21, 2017 · 6 comments

Comments

@helfi92
Copy link

helfi92 commented Feb 21, 2017

Does react-router v4 support Hot Module Replacement with react-hot-loader v3? I can't seem to find any documentation on that. If someone can help, I'd be willing to update the docs.

@mjackson
Copy link
Member

It should, yes. Though I don't know anyone actually doing it yet. You can be the pioneer :)

The blocker before was that all of our configuration was specified up front in one spot. But now that our API is Just Components™ anything that you can do with other components you should be able to do with React Router. Please give it a shot and let us know how it goes.

@hampusohlsson
Copy link

hampusohlsson commented Feb 23, 2017

@helfi92 @mjackson can confirm it is working, I'm using react-router v4 webpack v2 and react-hot-loader v3beta-6 😄

edit: code sample https://gist.github.com/hampusohlsson/9b5ab224f2856b7446892cff0576e78d

@helfi92
Copy link
Author

helfi92 commented Feb 23, 2017

@hampusohlsson @mjackson That's right, it worked liked a charm :)

@rclai
Copy link

rclai commented Mar 29, 2017

Does it work if you're using lazy loaded routes?

@helfi92
Copy link
Author

helfi92 commented Mar 29, 2017

@rclai It should work. The lazy loaded library seems to use System.import. You can do this:

// App.js
// ...

const Foo = asyncComponent(() => System.import('./components/Foo').then(module => module.default));
const Bar = asyncComponent(() => System.import('./components/Bar').then(module => module.default));
const Home = asyncComponent(() => System.import('./components/Home').then(module => module.default));

const App = () => (
  <Router>
    <div className="App">
      <ul>
        <li><Link to="/">Home</Link></li>
        <li><Link to="/foo">Foo</Link></li>
        <li><Link to="/bar">Bar</Link></li>
      </ul>

      <Route exact path="/" component={Home} />
      <Route path="/foo" component={Foo} />
      <Route path="/bar" component={Bar} />
    </div>
  </Router>
);

// ...
// asyncComponent.js
import React from 'react';

const asyncComponent = getComponent => class AsyncComponent extends React.Component {
  constructor(props) {
    super(props);

    this.Component = null;
    this.state = { Component: AsyncComponent.Component };
  }

  componentWillMount() {
    if (!this.state.Component) {
      getComponent().then(Component => {
        AsyncComponent.Component = Component;

        this.setState({ Component });
      });
    }
  }

  render() {
    const { Component } = this.state;

    if (Component) {
      return <Component {...this.props} />;
    }

    return null;
  }
};

export default asyncComponent;

@helfi92
Copy link
Author

helfi92 commented Apr 4, 2017

I created react-router-starter, a starter kit, that uses react-router v4 + router code splitting + hot module replacement. Feel free to use.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants