forlooptanzania / ride-my-way

Car pooling app (of developers) by developers for developers in Tanzania
22 stars 6 forks source link

Can not update value of state #17

Closed nelsonfrank closed 5 years ago

nelsonfrank commented 5 years ago

I was trying to call this.state.name on the function handleChange then try to call the function on Button OnClick event

state = {
     name: "Leo"
}
handleChange(){
      this.state.name;
}
render(){
    return(
        <button onClick={handleChange}> Click Me</button>
    );
}
karimkawambwa commented 5 years ago

@Nelsonfrank you will have to bind the context to that function.

So:

<button onClick={handleChange.bind(this)}> Click Me</button>

You can arrow this by using the // ES6 arrow function to capture this context:

handleChange = () => {...}