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

325
Vistas
Is there a way to use `event.target` to target specific sub-components and alter their style?

Is there a way to use event.target to target specific sub-components and alter their style?

export default function Component(props) {
  function MouseOver(event) {
    event.target.style.boxShadow =
      event.target.className === "container" &&
      "4px 6px 4px rgba(0, 0, 0, 0.5)";
    event.target.className.style.color = "#000";
  }
  function MouseOut(event) {
    event.target.style.boxShadow = "none";
    event.target.className.style.color = "#fff";
  }

  return (
    <div
      style={{
        ...Styles.root,
        background: `url(${props.image})`,
        backgroundSize: "cover",
        ...props.style,
      }}
      onMouseOver={MouseOver}
      onMouseOut={MouseOut}
      className={"container"}
    >
      <span style={Styles.title} className={"title"}>
        {props.title}
      </span>
    </div>
  );
}

const Styles = {
  root: {
    display: "flex",
  },
  title: {
    width: "100%",
    display: "flex",
    color: "#FFF",
    boxShadow: "none",
  },
};

I want the title to change color when the mouse is over the component. Can event.target be used to target specific sub-components and alter their style?

This is the error that I get

categoriesCard.js:10 Uncaught TypeError: Cannot set properties of undefined (setting 'color')
    at MouseOut (component.js:10:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4161:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4210:1)
    at invokeGuardedCallback (react-dom.development.js:4274:1)
    at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4288:1)
    at executeDispatch (react-dom.development.js:9038:1)
    at processDispatchQueueItemsInOrder (react-dom.development.js:9070:1)
    at processDispatchQueue (react-dom.development.js:9083:1)
    at dispatchEventsForPlugins (react-dom.development.js:9094:1)
    at react-dom.development.js:9285:1
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

"event.target" in your code is "MouseEvent" so it's not possible to change styles

why don't you add class name by using useState ?

export default function Component(props) {
  const [onMouseOver, setOnMouseOver] = useState(false);
  function MouseOver(event) {
    setOnMouseOver(true);
    // event.target.style.boxShadow =
    //   event.target.className === 'container' &&
    //   '4px 6px 4px rgba(0, 0, 0, 0.5)';
    // event.target.className.style.color = '#000'; # Write this code in YourOwnClassName class
  }
  function MouseOut(event) {
    setOnMouseOver(false);
    // event.target.style.boxShadow = 'none';
    // event.target.className.style.color = '#fff'; # Write this code in YourOwnClassName class
  }

  return (
    <div
      style={{
        ...Styles.root,
        background: `url(${props.image})`,
        backgroundSize: 'cover',
        ...props.style,
      }}
      onMouseOver={MouseOver}
      onMouseOut={MouseOut}
      className={onMouseOver ? 'container YourOwnClassName' :'container'}
    >
      <span style={Styles.title} className={'title'}>
        {props.title}
      </span>
    </div>
  );
}

actually, the greatest way to style is using "hover" css selector

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