GithubHelp home page GithubHelp logo

isabella232 / react-jss-hmr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cssinjs/react-jss-hmr

0.0 0.0 0.0 174 KB

Hot module replacement for react-jss

License: MIT License

JavaScript 94.88% HTML 5.12%

react-jss-hmr's Introduction

Hot module replacement with JSS and React

React-JSS-HMR enhances react-jss to support hot CSS updates in conjunction with react-hot-loader.

🤔 Do you really need this?

Hot reloading is possible (and much simpler) without this package. This package is only necessary if you are using react-hot-loader. And, you probably don’t need react-hot-loader either. Consider this quote from Dan Abramov (the creator of react-hot-loader):

However if you ... use something like Redux, I strongly suggest you to consider using vanilla HMR API instead of React Hot Loader, React Transform, or other similar projects. It’s just so much simpler—at least, it is today.

With Redux, “vanilla” HMR looks something like this:

const store = createStore(getPreloadedState())
const rootEl = document.getElementById('root')

const render = App => (
  <ReduxProvider store={store}>
    <JssProvider>
      <App />
    </JssProvider>
  </ReduxProvider>
)

ReactDOM.hydrate(render(App), rootEl)

if (module.hot) {
  module.hot.accept('./components/App', function () {
    const App = require('./components/App').default
    ReactDOM.render(render(App), rootEl)
  })
}

Because your Redux state is retained when the hot reload occurs, these hot reloads look just as 🔥.

If you’re switching away from react-hot-loader (even just to try out the above vanilla setup), don’t forget to delete its babel plugin (react-hot-loader/babel) from your configuration!

Install

Webpack v4

npm install --save-dev react-jss-hmr react-hot-loader
yarn add -D react-jss-hmr react-hot-loader

Webpack v3

Webpack v3 is no longer supported as of version 1.0.0, so you’ll need to install version 0.1.x.

npm install --save-dev react-jss-hmr@^0.1.3 react-hot-loader
yarn add -D react-jss-hmr@^0.1.3 react-hot-loader

Usage

First, set up react-hot-loader and make sure it’s working in your project.

Then, add the following to your webpack development config:

const ReactJssHmrPlugin = require('react-jss-hmr/webpack')
//...
module.exports = {
  //...
  resolve: {
    plugins: [
      new ReactJssHmrPlugin()
    ]
  }
}

⚠️ NOTE: the plugin is added to the resolve section in the webpack config, not the main plugins section!

Now you should be able to tweak the CSS rules in your injectSheet() calls and see the changes reflected in your browser without reloading.

Contributing

See our contribution guidelines.

License

MIT

react-jss-hmr's People

Contributors

felthy avatar raghav-maheshwari avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.