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

150
Visualizações
React hooks onHover modal on each array item

The modal(green box) needs to appear under each seperate red box once there is an onHover event. at the moment the green box is only appearing in the same area. so it would be a modal opening under each different red box. thanks in advance

sandbox here https://codesandbox.io/s/modal-array-ccpso3?file=/src/App.js:0-1283

import React, { useState } from "react";
import roundFace from "../src/sad.png";

const data = [
  { image: roundFace, number: 1 },
  { image: roundFace, number: 2 },
  { image: roundFace, number: 3 }
];

export const Items = (imageUrl, heading) => (
  <div className="flex flex-col">
    <div className="px-2 py-2 mx-auto align-middle">
      <div className="w-16 h-16 border-solid border-4 border-red-600">
        <img className="" src={imageUrl} alt="" />
        <div className={heading} />
      </div>
    </div>
  </div>
);
export const App = () => {
  const [Hover, setHover] = useState(false);

  return (
    <div className="">
      <div className="grid gap-x-8 gap-y-4 grid-cols-3 px-2 py-2">
        {data &&
          data.length > 0 &&
          data.map((box, index) => (
            <div key={`-${index}`}>
              <div
                onMouseEnter={() => setHover(true)}
                onMouseLeave={() => setHover(false)}
              >
                <Items imageUrl={box.image} />
              </div>
              {box.number}
            </div>
          ))}
        {Hover && (
          <p className="w-16 h-16 border-solid border-4 border-green-600">
            Hi!
          </p>
        )}
      </div>
    </div>
  );
};

export default App;

import * as React from "react";
import { render } from "react-dom";

import App from "./App";

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

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

0

I modified the App.js and Here it is.

import React, { useState } from "react";
import roundFace from "../src/sad.png";

const data = [
  { image: roundFace, number: 0 },
  { image: roundFace, number: 1 },
  { image: roundFace, number: 2 }
];

export const Items = (imageUrl, heading) => (
  <div className="flex flex-col">
    <div className="px-2 py-2 mx-auto align-middle">
      <div className="w-16 h-16 border-solid border-4 border-red-600">
        <img className="" src={imageUrl} alt="" />
        <div className={heading} />
      </div>
    </div>
  </div>
);
export const App = () => {
  const [Hover, setHover] = useState(null);

  return (
    <div className="">
      <div className="grid gap-x-8 gap-y-4 grid-cols-3 px-2 py-2">
        {data &&
          data.length > 0 &&
          data.map((box, index) => (
            <div key={`attendees-${index}`}>
              <div
                onMouseEnter={() => setHover(index)}
                onMouseLeave={() => setHover(false)}
              >
                <Items imageUrl={box.image} />
              </div>
              {box.number}
              <span>{Hover === box.number ? <p className="w-16 h-16 border-solid border-4 border-green-600">Wow</p> : ""}</span>

              {/* {Hover && (
              )} */}
            </div>
          ))}
      </div>
    </div>
  );
};

export default App;

I'm hovering on the 2nd Box and the Modal is appearing.

Is this what you're looking for?

enter image description here

Things I have changed:

  • Number value in the data array
  • UseState to null

Why did I change it?

The array starts from 0 so, I'm checking the box.number is equal to the Index number(Which is present in Hover). If yes, the box will appear.

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