Fast Refresh with Expo Web! šŸƒšŸ»ā€ā™‚ļøšŸ”„

Evan Bacon - Apr 20 '20 - - Dev Community

Alt Text

If you've used Expo on iOS or Android recently you've probably used Fast Refresh (by Dan Abramov) to achieve stateful hot reloading during development. But how do you use Fast Refresh with Expo for web.?.. (Pretty easily).

On web this will update the DOM without reloading the page, this means the state will remain the same across updates. To fully update you can simply reload the window with āŒ˜ + R.

šŸ¤” How to use

Currently there's no official Fast Refresh Webpack plugin, but you can get started today using a great community plugin by Michael Mok!

  • Bootstrap a new universal React Native project:
    • Expo: expo init then select any project
    • Other: npx create-react-native-app
  • Install the community Fast Refresh package:
    • yarn add -D @pmmmwh/react-refresh-webpack-plugin webpack-hot-middleware
  • Eject the Webpack config:
    • expo customize:web
  • In your newly created webpack.config.js:
const createExpoWebpackConfigAsync = require("@expo/webpack-config");
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");

module.exports = async function (env, argv) {
  const config = await createExpoWebpackConfigAsync(env, argv);

  // Use the React refresh plugin in development mode
  if (env.mode === "development") {
    config.plugins.push(
      new ReactRefreshWebpackPlugin({ disableRefreshCheck: true })
    );
  }

  return config;
};
Enter fullscreen mode Exit fullscreen mode
  • Now in your babel.config.js:
module.exports = function (api) {
  // This caches the Babel config by environment.
  api.cache.using(() => process.env.NODE_ENV);

  return {
    presets: ["babel-preset-expo"],
  };
};
Enter fullscreen mode Exit fullscreen mode
  • Now run expo start:web to use it!

šŸ’” BTW

When the official React fast refresh has been released, we will work on unifying it with native to create a universal solution. Until then this is a pretty nifty little feature! :]

Why share now then?

I got the idea for this tutorial from my friend Tim Neutkens of Next.js. I highly recommend using Next.js with Expo for web especially for navigation!

šŸ‘‹ That's all

Thanks for reading, that's all I've got for you today. Let me know if you enjoyed this article, and reach out if you have any more questions!

. . . . . . . . . . . . . . . . .