Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda