Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

80
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda