question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HMR and React with Mix

See original GitHub issue
  • Laravel Mix Version: 1.4.5 (npm list --depth=0)
  • Node Version (node -v): 7.3.0
  • NPM Version (npm -v): 3.10.10
  • OS: Windows 10

Description:

Hi, I’m trying to run Hot reloading for my react assets for a long time but without any success. I was trying many configurations from GH and nothing works. Could you help / share your config and tell what exactly should I do for enable it.

My webpack.mix.js:

mix.webpackConfig({
    devtool: "source-map", devServer: {
        contentBase: path.resolve(__dirname, 'public'),
    },
    module: {
        loaders: [{
            test: /\.js$/,
            loaders: ['react-hot-loader/webpack', 'babel'],
        }]
    },
    entry: {
        'app': [
            'babel-polyfill',
            'react-hot-loader/patch',
            './resources/assets/js/app'
        ]
    }
});

mix.react('resources/assets/js/app.js', 'public/js').sourceMaps(true);
mix.sass('resources/assets/sass/app.scss', 'public/css').sourceMaps(true);

.babelrc:

{
  "plugins": ["transform-decorators-legacy", "transform-class-properties", "transform-object-rest-spread", "react-hot-loader/babel"]
}

my app.js:

import 'react-hot-loader/patch';
import React from 'react'
import ReactDOM from 'react-dom'
import {AppContainer} from 'react-hot-loader';
import App from './components/App'
import './styles/app.scss'


const MOUNT_NODE = document.getElementById('app');

ReactDOM.render(
    <AppContainer>
        <App/>
    </AppContainer>
    ,
    MOUNT_NODE
);
if (process.env.NODE_ENV === 'development' && module.hot) {
    module.hot.accept();
}

And hot script in package.json:

"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --output-public-path=public/ --config=node_modules/laravel-mix/setup/webpack.config.js",

Generally hot script starts without errors, and it says that compiled successfully, I am on app.dev and js file is loaded from localhost:8080, but there are no any effect in browser, also there are nothing about HMR in console. Watching files with browserSync works well but it reloads full page, but I want to achieve hot reload.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:8

github_iconTop GitHub Comments

2reactions
Wchindanaicommented, Dec 12, 2017

Do you have any solutions for this issue ?

0reactions
RibikiHcommented, Dec 3, 2021

In my laravel project it working now Package use:

  • react-hot-loader@4.13.0
  • babel-plugin-transform-class-properties@6.24.1
  • babel-preset-react@6.24.1

and my mix config

const mix = require('laravel-mix');


mix.ts('resources/ts/app.tsx', 'public/js')
    .postCss('resources/css/app.css', 'public/css', [
        require("tailwindcss"),
        require("autoprefixer"),
    ]);`

mix.babelConfig({
    presets: [
        [
            '@babel/preset-env',
            {
                modules: 'auto',
                targets: { node: 'current' }
            },
            '@babel/preset-react',
        ]
    ],
    plugins: [
        [
            'react-hot-loader/babel',
            '@babel/plugin-proposal-class-properties',
            {
                loose: true
            }
        ]
    ]
})

App.tsx

import React from 'react';
import {Redirect, Route, Router, Switch} from "react-router-dom";
import { history } from "../helper/history";
import { routes } from "../helper/routes";
import {CustomRoute} from '../components';
import { hot } from 'react-hot-loader/root';

const App = () => (
    <>
        <Router history={history}>
            <Switch>
                { routes.map((route, i) =>
                    <CustomRoute key={i} {...route} />
                ) }
                <Redirect from="*" to="/"/>
            </Switch>
        </Router>
    </>
)
export default hot(App);

Hope it helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HMR and React with Mix · Issue #1222
Hi, I'm trying to run Hot reloading for my react assets for a long time but without any success. I was trying many...
Read more >
Laravel Mix Hot Module Reloading With React
Laravel Mix Hot Module Reloading With React. Interested in a boilerplate with HMR included amongst React Router, Typescript support and more ...
Read more >
Hot Module Replacement | Laravel Mix Documentation
Hot Module Replacement (or Hot Reloading) allows you to, not just refresh the page when a piece of JavaScript is changed, but it...
Read more >
Using Webpack's Hot Module Replacement with React
Out of the box, webpack supports a fancy new alternative to live reload to see if your changes worked called hot module replacement,...
Read more >
Setting Up Webpack for React and Hot Module Replacement
Now that we have a basic webpack configuration set up we can add React to the mix. Let's install React and write a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found