gpt4 book ai didi

reactjs - Webpack 热模块重新加载器无法与 React 无状态组件一起使用

转载 作者:行者123 更新时间:2023-12-03 13:51:43 24 4
gpt4 key购买 nike

我在使用 webpack-dev-middleware 时遇到了一些麻烦,不能使用 React 无状态函数进行热重载,但如果我创建一个类扩展组件,则工作正常。

例如,这非常有效。

// home.js

import React from 'react'

export default class Home extends React.Component {
render() {
return (
<div>
<h1>Drop it like it's hot</h1>
</div>
)
}
}

但是,这失败了。

// home.js

import React from 'react'

export default function Home() {
return (
<div>
<h1>Hello World</h1>
</div>
)
}

错误:

[Warning] [HMR] The following modules couldn't be hot updated: (Full reload needed) (bundle.js, line 1742)
This is usually because the modules which have changed (and their parents) do not know how to hot reload themselves. See http://webpack.github.io/docs/hot-module-replacement-with-webpack.html for more details.
[Warning] [HMR] - ./client/components/home.js (bundle.js, line 1750)

最佳答案

如果它仍然相关并且人们还存在问题,那么有一种方法,即仍在开发中的版本 3。我将在此处粘贴我的 package.json 和 webpack.config.js 文件的设置,但如果您想要一个工作示例 here is the repo I created for this

package.json

{
"name": "favesound-project",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --progress --colors --hot --config ./webpack.config.js"
},
"devDependencies": {
"babel-core": "^6.0.20",
"babel-loader": "^6.0.1",
"babel-preset-es2015": "^6.0.15",
"babel-preset-react": "^6.0.15",
"babel-preset-stage-0": "^6.0.15",
"chai": "^3.5.0",
"enzyme": "^2.3.0",
"exports-loader": "^0.6.3",
"imports-loader": "^0.6.5",
"jsdom": "^9.2.1",
"mocha": "^2.5.3",
"react-addons-test-utils": "^15.1.0",
"react-hot-loader": "^3.0.0-beta.0",
"webpack": "^1.12.2",
"webpack-dev-server": "^1.12.1"
},
"dependencies": {
"react": "^15.1.0",
"react-dom": "^15.1.0",
"react-redux": "^4.4.5",
"react-router": "^2.4.1",
"react-router-redux": "^4.0.5",
"redux": "^3.5.2",
"redux-logger": "^2.6.1",
"redux-thunk": "^2.1.0",
"soundcloud": "^3.1.2",
"whatwg-fetch": "^1.0.0"
},
"author": "",
"license": "ISC",
"keywords": [],
"description": ""
}

webpack.config.js

const webpack = require('webpack');

module.exports = {
entry: [
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
'./src/index.js'
],
module: {
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loaders: ['babel']
}]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
output: {
path: __dirname + '/dist',
publicPath: '/',
filename: 'bundle.js'
},
devServer: {
contentBase: './dist',
hot: true
}
};

您可以使用我创建的存储库作为样板,并在生产时升级react-hot-loader版本。干杯。

关于reactjs - Webpack 热模块重新加载器无法与 React 无状态组件一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36146151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com