Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
Open single popup on table row click

I am stuck in a issue,

I have created one custom table, in each td of table row it has a action button, onclick() of action it open a small menu.

But it never closes the pervious open menu, in-fact open multiple menu.

How to close previous and open only one menu.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here is the code, which may help you:

import React, { useRef, useEffect, useState } from "react";

export default function App() {
  const clickOutside = useRef(null);
  const [showHideMenu, setShowHideMenu] = useState(false);

  const handleClickOutside = (event, ref) => {
    if (ref.current && !ref.current.contains(event.target)) {
      if (showHideMenu) setShowHideMenu(!showHideMenu);
    }
  };

  useEffect(() => {
    document.addEventListener("mousedown", (event) =>
      handleClickOutside(event, clickOutside)
    );
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  });
  return (
    <div ref={clickOutside}>
      <h1>Hello CodeSandbox</h1>
      <button onClick={() => setShowHideMenu(!showHideMenu)}>Show Hide</button>
      <button onClick={() => setShowHideMenu(!showHideMenu)}>Show Hide</button>
      <button onClick={() => setShowHideMenu(!showHideMenu)}>Show Hide</button>
      <div>{showHideMenu ? "show" : "hide"}</div>
    </div>
  );
}

Actually, this code works as you want. Whenever you click outside of your button, it'll automatically close all other popups.

Here is the code sandbox:

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!