Skip to content Skip to sidebar Skip to footer

(this) Is Recognised On React Js Callback

class App extends Component { constructor(props){ super(props); this.state = { histories: [ { 'name': 'Test1

Solution 1:

You should pass arrow function as success callback for AJAX call inside onFormSubmit method to make sure that this inside this callback points to the component:

onFormSubmit(id) {
        console.log("Started");
        fetch("http://localhost:xxxx/api/Surveys/" + id + "/submit", {
            method:'POST'
        })
        .then((response) => {
             ...
        })

}

Solution 2:

You try to set the "histories" in the onFormSubmit function. To access "this" of the component, you need to bind it to the function, i.e.

onFormSubmit {
.... 
}.bind(this)

or in the constructor

this.onFormSubmit = this.onFormSubmit.bind(this)

Post a Comment for "(this) Is Recognised On React Js Callback"