Skip to content Skip to sidebar Skip to footer

React 'this' Undefined When Adding Table Row

I'm attempting to add a new row of data to a table that occurs when a user inputs text into a field and clicks a button. The button click is tied to a function (AddNewRow) which s

Solution 1:

Use arrow function to make this available in the then function:

axios
  .post('/Controller/CreateJob', { Name: this.refs.NewJobName.value })
  .then((response) => {
    if (response.data.Error) {
      window.alert(response);
    } else {
      this.setState(prevState => ({ 
        tableData: prevState.tableData.concat([response]) 
      }));
    }
  });

Post a Comment for "React 'this' Undefined When Adding Table Row"