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

157
Vistas
Writing a program in react when mouseover on parent element add child element to coressponding parent element

I want to write a program in react jsx when mouseover on an element suppose <li>

<li><a>Pakistan cricket won the match</a></li>
<li><a>We should exercise every day</a></li>

and add these childs elements to coressponding <li> element

<img src='...' />
<p>details  of header</p>
<button>add to favorite</button>

When mouseout from an <li> element the coressponding childs should be removed

Note:when mouseover, the childs elements should be workable

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

So basically you need to have onMouseEnter and onMouseLeave on parent elements and make a state that holds whether to show the additional data here is a simple example

import "./styles.css";
import {useState} from 'react'

function Children(){
  return (
    <>
        <img src='https://picsum.photos/200/300' />
        <p>details  of header</p>
        <button>add to favorite</button>
    </>
  )
}

function Parent() {
  const [isMouseOver,setIsMouseOver] = useState(false)

  const handleEnter = () => {
    setIsMouseOver(true)
  }

  const handleLeave = () => {
    setIsMouseOver(false)
  }
  return(
    <div onMouseEnter={handleEnter} onMouseLeave={handleLeave}>
      <h1>Hover me</h1>
      {isMouseOver ?
      <>
        <Children />
      </>:
      null
      
    }
    </div>
  )
}

export default function App() {
  return (
    <div className="App">
      <Parent />
    </div>
  );
}

Similarly, you would change the Parent component to your li and the additional data that should be visible to Children Component.

Note that in this example h1 is a block component so you should leave the whole block to make the data disappear

demo at : https://codesandbox.io/s/silly-dewdney-d8btf?file=/src/App.js:0-738

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can add some class and in its respective CSS, handle the mouseover (:hover).

App.js

import "./styles.css";

function ListItem({i})
{
  return (
    <li className="listitem">
      <a href="stackoverflow.com">Link{i}</a>
      <img className="hideele" src="" alt={i} />
      <p className="hideele">Para{i}</p>
      <button className="hideele">Button{i}</button>
    </li>
  );
}

export default function App() {
  return (
    <div className="App">
      <ul>
        <ListItem i={1} />
        <ListItem i={2} />
        <ListItem i={3} />
      </ul>
    </div>
  );
}

Index.js

import { StrictMode } from "react";
import ReactDOM from "react-dom";

import App from "./App";

const rootElement = document.getElementById("root");
ReactDOM.render(
  <StrictMode>
    <App />
  </StrictMode>,
  rootElement
);

CSS:

.App {
  font-family: sans-serif;
}
.listitem:hover .hideele {
  display: block;
}
.hideele {
  display: none;
}
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