János Simonyi János Simonyi - 3 months ago 47
React JSX Question

React and redux webpack config error

I'm pretty new in this. I trying to learn react/redux and i have an error in my code.

The error is on the commented line, exactly the first dot:

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;
};


If i remove the comment, i get this error by 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};


My webpack config:

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()
]
}


How can i fix this error? If i well informed, three dots are in ES6 and i can use the ES6 anonymous function syntax without error. What do i miss?

I just copied the config file, i think the problem is there. I installed stage-0 preset with npm, someone who had the same problem it fixed for him.

My 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"
}

Answer

The object spread used in the

const newstate = {...state};

statement is not standardised yet.

And as of today it's a part of stage-2 (see https://github.com/tc39/proposals)

So, for it to work you need to plug the stage-2 preset.

Alternatively you might have used Object.assign() that is a part of the ES2015 already:

const newstate = Object.assign({}, state);