1

When I try to get value ...this.state.users shows error in react

React Code

handleChange(i, e) {
        const { name, value } = e.target;
        let users = [...this.state.users];
        users[i] = { ...users[i], [name]: value };
        this.setState({ users });
    }

My webpack file

const path = require('path');
module.exports = (env) => {
    const isProduction = env === 'production';
    return {
        entry: './src/index.js',
        output: {
            path: path.join(__dirname, 'public'),
            filename: 'bundle.js',
        },
        module: {
            rules: [
                {
                    loader: 'babel-loader',
                    test: /\.js$/,
                    exclude: /node_modules/,
                },
                {
                    test: /\.css$/,
                    use: ['style-loader', 'css-loader'],
                },
            ],
        },
    };
}

And the error is

 ERROR in ./src/components/Index.js
[1] Module build failed: SyntaxError: E:/Program/reactjs/check/client/src/components/Index.js: Unexpected token (47:15)

Please help me to fix this error

1
  • You sure this.state.users is spreadable? Maybe check if its an object or array before spreading. Commented Feb 27, 2019 at 0:19

1 Answer 1

1

It looks like you need to configure babel correctly. According to this post, you need to make sure that you have the stage-0 preset installed:

npm install --save-dev babel-preset-stage-0

and configured:

{
  "presets":[
    "es2015", "react", "stage-0"
  ]
}

However, if you are using Babel v7 or higher you have to use a different installation & configuration:

npm install --save-dev @babel/plugin-proposal-object-rest-spread

and put it into your .babelrc file:

{
  "plugins": ["@babel/plugin-proposal-object-rest-spread"]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.