简体   繁体   English

Webpack react-hot-loader无法正常工作

[英]Webpack react-hot-loader not working

Below is my webpack.config.js code 下面是我的webpack.config.js代码

var webpack = require('webpack');
var path = require('path');

module.exports = {
    // context: __dirname + "/app",
   entry: [
    'webpack-dev-server/client?http://0.0.0.0:8080',
    'webpack/hot/only-dev-server',
     './src/main.jsx'],
    output: {
        path: "./build",
        filename: "main.js"
    },
    module: {
      loaders: [
        {
          test: /\.jsx?$/,
          exclude: /(node_modules|bower_components)/,
          loaders:  ['react-hot', 'babel'],
          include: path.join(__dirname, 'build'),
          query:
            {
              presets:['es2015', 'react']
            },
          plugins: [
            new webpack.HotModuleReplacementPlugin()
          ]
        }
      ]
    }
}

And this is script code of my package.son 这是我的package.son的脚本代码

  "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "build": "webpack",
  "dev": "webpack-dev-server --devtool eval --progress --colors --hot --content-base build"
  },

And this is my main.js code 这是我的main.js代码

var React = require('react');
var ReactDOM = require('react-dom');

ReactDOM.render(
  <h1>Hello World!</h1>,
  document.getElementById('example')
);

When I type "npm run dev" , I got this error 当我输入“ npm run dev”时,出现此错误

ERROR in ./src/main.jsx
Module parse failed: /Users/testaccount/Documents/React/test-react/src/main.jsx Line 6: Unexpected token <
You may need an appropriate loader to handle this file type.
| 
| ReactDOM.render(
|   <h1>Hello World!</h1>,
|   document.getElementById('example')
| );
 @ multi main

And I go to localhost:8080 and nothing show. 我去了localhost:8080,什么也没显示。 Anyone knows what happened? 有人知道发生了什么吗? Why my react-hot-loader is not working? 为什么我的react-hot-loader无法正常工作?

I made it work using the solution found here . 我使用这里找到的解决方案使其工作。 Basically what needs to be done is add module.hot.accept() to the script where you render your component to. 基本上需要做的是将module.hot.accept()添加到脚本中,将组件呈现到该脚本。

For example: 例如:

import React from 'react';
import ReactDOM from 'react-dom';
import MainLayout from './components/MainLayout.jsx';

ReactDOM.render(
  <MainLayout />,
  document.getElementById('root')
);

module.hot.accept(); // <-- this one.

Refer to react-hot-boilerplate github and check .babelrc file in the github. 请参考react-hot-boilerplate github并检查github中的 .babelrc文件。

Maybe I don't, also check include , query , 0.0.0.0 in your settings. 也许我不知道,还请检查您的设置中的includequery0.0.0.0

Remember, as per your babel version(5 or 6), settings have to be different. 请记住,根据您的babel版本(5或6),设置必须不同。

And now react-hot-loader is deprecation. 现在,react-hot-loader已弃用。 Refer to react-hot-loader github . 请参考react-hot-loader github

By default react js provides hot reloading feature. 默认情况下,react js提供热重载功能。 Still if it's not working: 仍然无法正常工作:

Try adding -- --reset-cache to your run command. 尝试在运行命令中添加-- --reset-cache *for Linux OS *对于Linux OS

Ref: React js hot reload issue fix for Linux OS 参考: 针对Linux OS的React js热重装问题修复

Duplicate issue in github github中的重复问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM