React and redux webpack config error

reactjs webpack redux

485 просмотра

2 ответа

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"
  }
Автор: János Simonyi Источник Размещён: 08.11.2019 11:01

Ответы (2)


0 плюса

Решение

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);
Автор: zerkms Размещён: 20.08.2016 08:27

0 плюса

Like @zerkms say: Object spread ({...state}) is stage-2 at the moment (github.com/tc39/proposals), so you need to add the stage-2 to your config.

Solution:

You have to include the stage-2 in your dev dependencies

npm install -dev babel-preset-stage-2

Then include in your web pack config

...
loaders: ['react-hot', 'babel?presets[]=react,presets[]=es2015,presets[]=stage-2']
...
Автор: MatCas Размещён: 29.10.2016 12:10
Вопросы из категории :
32x32