Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
ReactJs - can't access setState inside axios call

I'm having troubles setting state to the variable isCorrectAnswer inside an axios call. I'm getting the error Cannot read properties of undefined (reading 'setState') in the console log. What am I doing wrong? Initial state of isCorrectAnswer variable is constructor(props) {super(props)this.state = { isCorrectAnswer: false }}
and the axios call is

axios.post(
      "API/path/to/ressource",
      postData,
      {
        headers: {
          "X-Access-Token":
            "token",
        },
      }
    )
      .then(function (res) {
        this.setState({
          isCorrectAnswer: res.data.correct //Here im trying to change the state
        }, () => console.log(this.state.isCorrectAnswer)) //and logging into console
      })
      .catch(function (error) {
        console.log(error);
      });
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

When you call a function (non arrow function), this is always an implicit parameter.

Normal functions

By normal functions I mean functions that are not methods.

In strict mode value of this is always undefined.

And in non strict mode value of this is always the global object (window in browsers)

function foo() {
  "use strict";
  return this;
}

function bar() {
  return this;
}

console.log(foo() === undefined); // true
console.log(bar() === window); // true

Methods

this refers to the object on which the method has been invoked.

function foo() {
  "use strict";
  return this;
}

const obj = { foo };

console.log(obj.foo() === obj); // true

And in your case the callback passed to then is a normal function (not a method), so it's this is set undefined, hence you are getting an error.

Update to an arrow function and it would solve the issue because arrow functions don't have their own this, in arrow functions this is decided on the basis of lexical scope. Checkout the example below:

const getRandomNums = (delay) =>
  new Promise((res) => setTimeout(() => res([5, 2, 3]), delay));

class App extends React.Component {
  constructor() {
    super();
    this.state = { nums: [] };
  }

  componentDidMount() {
    getRandomNums(1500).then((nums) => {
      this.setState({ nums });
    });
  }

  render() {
    return <div>{JSON.stringify(this.state.nums, null, 2)}</div>;
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<div id="root"></div>

about 4 years ago · Juan Pablo Isaza Report

0

You can also do Use arrow function instead of function

axios.post(
  "API/path/to/ressource",
  postData,
  {
    headers: {
      "X-Access-Token":
        "token",
    },
  }
)
  .then((res) => {
    this.setState({
      isCorrectAnswer: res.data.correct //Here im trying to change the state
    }, () => console.log(this.state.isCorrectAnswer)) //and logging into console
  })
  .catch((error) => {
    console.log(error);
  });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!