Is usage of binding neccsary

Tell us what’s happening:
when i try this exercise in my react app(the code is in vs code not in fcc)
when i use bind method as shown below
this.handleClick = this.handleClick.bind(this);
the following errors come
“message”: “Unexpected token. A constructor, method, accessor, or property was expected.”,
“message”: “’;’ expected.”,
“message”: “Declaration or statement expected.”
it gives an error ,but when its removed it works fine ?should i need to worry about or just ignore
Your code so far

import React, { Component } from 'react';


class Xtreme extends Component {
    constructor(props) {
        super(props);
    this.state = {
        name: 'freeCodeCamp'
      }
    }
    //this.handleClick = this.handleClick.bind(this);
    handleClick =() => {
    this.setState({
        name:'freecodecamp rocks'
    })
    }
   render(){
  const name = this.state.name;
    return (
    <div className="Xtreme">
        <p>{name}</p>
        <p>{this.state.name} again</p> 
        <button onClick={this.handleClick}>change me</button>
       
    </div>
  );
}
}
export default Xtreme;


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.

Challenge: Set State with this.setState

Function binding should go inside the constructor

1 Like

okay it works but do we really need it

You don’t need it, just remove the line and it will work because you’re using so-called class field in property = someValue; format, assigning arrow function to your handler.

1 Like