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

185
Visualizações
Where can I find documentation on why `this` context is undefined when assigning object method's as DOM event handlers in React?

It seems that React components change the behavior of how this is assigned in DOM event handlers, but I cannot find any documentation that details this behavior.

For example, when using an object's method as a DOM event handler with vanilla JS, the this context remains as the object:

function SomeClass() {}
SomeClass.prototype.showThis = function() { console.log(this) };
let o = new SomeClass();
<button onclick="o.showThis()">Show "this"</button>

React, however, changes this behavior such that the this context becomes undefined:

function SomeClass() {}
SomeClass.prototype.showThis = function() { console.log(this) };
let o = new SomeClass();

function App() {
  return (
       <button onClick={o.showThis}>Show this</button>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>

Where is the documentation that explains this? (I can surmise why this may occur, but would like to know where this is covered in the React docs.)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is not anything special to React. This is due to the fact that, with the inline handler, the moment the function gets invoked, it's being invoked as part of an object:

onclick="o.showThis()"
         ^ object
                   ^^ invocation

But in the React code, it's not being invoked as part of an object - rather, o.showThis is passed as a callback which is then called like any other callback - like callback() or something of the sort (and not as part of an object). If you very slightly tweak the code so that the showThis is invoked as part of an object, it'll show the same behavior as the inline handler:

onClick={() => o.showThis()}
               ^ object
                         ^^ invocation

function SomeClass() {}
SomeClass.prototype.showThis = function() { console.log(this) };
let o = new SomeClass();

function App() {
  return (
       <button onClick={() => o.showThis()}>Show this</button>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>

So there's nothing for React specifically to explain about it - it's a behavior of the underlying JavaScript.

You would similarly get undefined in the inline handler if the function was called not as part of an object, and you were in strict mode.

'use strict';

function SomeClass() {}
SomeClass.prototype.showThis = function() {
  console.log(this)
};
const o = new SomeClass();

const theFn = o.showThis;
<button onclick="theFn()">Show "this"</button>

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