Syntax Error. Unexpected token '=' no React

1

When I try to run dev-server it returns me an error in the console that I can not identify what is generating it

webpack.config.js

iconst path = require('path');

module.exports = {
entry: './src/app.js',
output: {
    path: path.join(__dirname,'public'),
    filename: 'bundle.js'
},
module: {
    rules: [{
        loader: 'babel-loader',
        test: /\.js$/,
        exclude: /node_modules/,
        query: {
            presets: ['es2015', 'react']
        }
    },]
},
devtool: 'cheap-module-evel-source-map',
devServer: {
    contentBase:path.join(__dirname,'public'),
    historyApiFallback: true
}
};

Error file

import React from 'react';

class FormCadastroUser extends React.Component {
constructor(props){
    super(props);

    this.onEmailChange = this.onEmailChange.bind(this);
    this.onNameChange = this.onNameChange.bind(this);
    this.onLastNameChange = this.onLastNameChange.bind(this);
    this.onPasswordChange = this.onPasswordChange.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
    this.state={
        name: '',
        lastname: '',
        email: '',
        password: ''
    }
}
onNameChange = (e) => {
    const name = e.target.value;
    this.setState(() => ({name}));
} 
onLastNameChange = (e) => {
    const lastname = e.target.value;
    this.setState(() => ({lastname}));
} 
onEmailChange = (e) => {
    const email = e.target.value;
    this.setState(() => ({email}));
} 
onPasswordChange = (e) => {
    const password = e.target.value;
    this.setState(() => ({password}));
} 
onSubmit = (e) => {
    e.preventDefaut();
    this.props.onSubmit({
        name: this.state.name,
        lastname: this.state.lastname,
        email: this.state.email,
        password: this.state.password
    });
}

render(){
    return (
        <div className= 'container'>
            <form onSubmit={this.onSubmit}>
                <div className="form-row">
                    <div className="col">
                        <input type="text" className="form-control" placeholder="First name" autoFocus value={this.state.name} onChange={this.onNameChange} />
                    </div>
                    <div className="col">
                        <input type="text" className="form-control" placeholder="Last name" value={this.state.lastname} onChange={this.onLastNameChange}/>
                    </div>
                    <div className="col">
                    <input type="Email"    className="form-control" placeholder="Email" value={this.state.email} onChange={this.onEmailChange}/>
                    </div>
                    <div className="col">
                    <input type="text" className="form-control" placeholder="Password" value={this.state.password} onChange={this.onPasswordChange}/>
                    </div>
                </div>
                <div style={{paddingTop: '10px'}}>
                <button type="submit" className="btn btn-primary">Sign in</button>
                </div>
            </form>
        </div>
    );

}
}
export default FormCadastroUser;

The error accuses the

onNameChange = (e) => { | ^

at the Equal sign.

    
asked by anonymous 28.10.2017 / 18:38

1 answer

1

Change these methods from onNameChange = (e) => { to onNameChange(e) { .

For two reasons:

  • arrow functions does not allow .bind to another context. That is, const a = () => console.log(this); a.bind('foo')(); will give window , meaning your idea of .bind in the constructor goes out of effect

  • class properties need more babel packages to configure and apparently this is not configured, otherwise it would not give an error.

28.10.2017 / 18:45