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

245
Visualizações
Why "this" is returning undefined from my functional component in ReactJS?

I'm still beginner in ReactJS and JavaScript and I'm having trouble using this.

I'm rendering a list of properties and when I do onMouseOver I'd like to get the element I'm hovering over with this. But it's returning undefined.

Can you tell me why I'm getting this error?

Here's my code I put into codesandbox.io

Thank you very much in advance.

enter image description here

import React from "react";
import "./styles.css";

import Property from "./components/Property";

import { mock } from "./data/mock";

export default function App() {
  const [data] = React.useState(mock.data.dora.exploreV3.sections[2].items);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      {data.map((item) => (
        <Property key={item.listing.id} item={item} />
      ))}
    </div>
  );
}

import React from "react";

const Property = ({ item }) => {
  return (
    <div
      style={{
        padding: "5px 0",
        cursor: "pointer"
      }}
      onMouseOver={() => console.log("item: ", this)}
    >
      {item.listing.name}
    </div>
  );
};

export default Property;

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

0

The reason your code does not work as you expect is because this is not bound to the element during rendering. You need to access the element using the MouseEvent target. Your Property component should look like this:

import React from "react";

const Property = ({ item }) => {
  return (
    <div
      style={{
        padding: "5px 0",
        cursor: "pointer"
      }}
      onMouseOver={(event) => console.log("item: ", event.target)}
    >
      {item.listing.name}
    </div>
  );
};

export default Property;

Alternatively, you could use React's useRef to do this. Here's a good article explaining how to implement that: https://linguinecode.com/post/get-current-element-react-onmouseover-or-onmouseenter

about 4 years ago · Juan Pablo Isaza Relatório

0

Fat arrow functions are not able to access "this" keyword. Try updating the function as a regular function.

This might be helpful : https://www.section.io/engineering-education/how-to-use-javascript-arrow-functions-and-this-keyword/

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