我是新手。我试图学习react / redux,我的代码中有错误。
错误在注释行上,恰好是第一个点:
const userReducer = (state={}, actions) => {
if(actions.type == 'name')
{
state.name = actions.value;
}else if(actions.type == 'age')
{
state.age = actions.value;
}
//const newstate = {...state};
return state;
};
如果我删除评论,我会通过webpack-dev-server收到此错误:
ERROR in ./src/index.js
Module build failed: SyntaxError: Unexpected token (13:19)
11 | state.age = actions.value;
12 | }
> 13 | const newstate = {...state};
我的webpack配置:
var webpack = require('webpack');
var path = require('path');
module.exports = {
devtool : 'inline-source-map',
entry : [
'webpack-dev-server/client?http://127.0.0.1:8080/',
'webpack/hot/only-dev-server',
'./src'
],
out : {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
resolve : {
modulesDirectories: ['node_modules', 'src'],
extensions: ['', '.js', '.jsx']
},
module : {
loaders : [
{
test: /\.jsx?$/,
exclude: /node_module/,
loaders: ['react-hot', 'babel?presets[]=react,presets[]=es2015']
}
]
},
plugins : [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
]
}
我该如何解决此错误?如果我知情,ES6中有三个点,我可以使用ES6匿名函数语法而不会出错。我错过了什么?
我刚刚复制了配置文件,我认为存在问题。我用npm安装了0阶段的预设,这个人为他修好了同样的问题。
我的package.json:
{
"name": "treeview",
"version": "0.0.0",
"description": "treeview",
"main": "javascript/main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.3.1",
"react-dom": "^15.3.1"
},
"devDependencies": {
"babel-core": "^6.13.2",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.13.2",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-0": "^6.5.0",
"react-hot-loader": "^1.3.0",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.14.1"
}
答案 0 :(得分:0)
中使用的对象传播
const newstate = {...state};
声明尚未标准化。
截至今天,它是stage-2
的一部分(参见https://github.com/tc39/proposals)
因此,要使其正常工作,您需要插入stage-2
预设。
或者您可能已经使用了Object.assign()
已成为ES2015的一部分:
const newstate = Object.assign({}, state);
答案 1 :(得分:0)
像@zerkms一样说:对象传播({... state})目前是阶段2(github.com/tc39/proposals),所以你需要将stage-2添加到你的配置中。
您必须在您的开发依赖项中包含stage-2
npm install -dev babel-preset-stage-2
然后包含在您的网络包配置
中...
loaders: ['react-hot', 'babel?presets[]=react,presets[]=es2015,presets[]=stage-2']
...