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

81
Visualizações
onClick function won't fire for mapped component

Here is the relevant code:

const Members = () => {
  // array of each video in selected grade
  const videosMap = (videos) => {
    return videos.map((video) => (
      <VideoCard
        key={video.id}
        thumbnail={video.thumbnail}
        title={video.title}
        description={video.description}
        onClick={() => {
          handleVideoClick();
        }}
      />
    ));
  };

  // updates state of shown videos & page heading
  const handleGradeButtonClick = (videos, heading) => {
    setShowVideos(videosMap(videos));
    setVideosHeading(heading);
  };

  const handleVideoClick = () => {
    console.log("test");
  };

  // controls state of which grade's videos to show
  const [showVideos, setShowVideos] = useState(videosMap(kinder_videos));
  // controls states heading to display depending on selected grade
  const [videosHeading, setVideosHeading] = useState("Kindergarten");

  const [showVideoDetails, setShowVideoDetails] = useState(null);

The handleVideoClick is the function that is not working when I click on one of the mapped VideoCard components.

Here is the full code if you want to see that: https://github.com/dblinkhorn/steam-lab/blob/main/src/components/pages/Members.js

When I look in React DevTools at one of the VideoCard components, it shows the following:

onClick: *f* onClick() {}

If I don't wrap it in an arrow function it does execute, but on component load instead of on click. I have a feeling it has something to do with my use of .map to render this component, but haven't been able to figure it out.

Thanks for any help!

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

0

There's no problem with your mapping method, you just need to pass the onClick method as a prop to your VideoCard component :

On your VideoCard component do this :

const VideoCard = (props) => {
  const { thumbnail, description, title, onClick } = props;

  return (
    <div className="video-card__container" onClick={onClick}>
      <div className="video-card__thumbnail">
        <img src={thumbnail} />
      </div>
      <div className="video-card__description">
        <div className="video-card__title">
          <h3>{title}</h3>
        </div>
        <div className="video-card__text">{description}</div>
      </div>
    </div>
  );
};

export default VideoCard;
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