title: how to close previous dropdown list and open the one on which we are clicking?
body: <p>I want to close the dropdown that is currently open when clicked outside of it. I have implemented the logic but I am facing some issues.</p>
  <p>I have 5 dropdowns on nav-bar initially everything is working fine the dropdown that is open is closing when clicked outside or when clicked on another dropdown. But after clicking on all the dropdowns the user has to click twice on the dropdown to open, on first click the dropdown that is open is closed, and on the second click dropdown is opening.</p>
  <p>I am not able to detect the problem. Below I have given the code for reference.</p>
  <p>Dropdown.tsx</p>
  <pre><code>
  function Dropdowns(){
  
      const [showPopup, setShowPopup] = useState({
      item1: false,
      item2: false,
      item3: false,
      item4: false,
      item5: false,
      item6: false,
    });
      
      const item1Ref = useRef(null);
    const item2Ref = useRef(null);
    const item3Ref = useRef(null);
    const item4Ref = useRef(null);
    const item5Ref = useRef(null);
    const item6Ref = useRef(null);
  
    useOutsideClick(item1Ref, () =&gt; {
      if (showPopup.item1) {
        setShowPopup({ ...showPopup, item1: false });
      }
    });
  
    useOutsideClick(item2Ref, () =&gt; {
      if (showPopup.item2) {
        setShowPopup({ ...showPopup, item2: false });
      }
    });
    useOutsideClick(item3Ref, () =&gt; {
      if (showPopup.item3) {
        setShowPopup({ ...showPopup, item3: false });
      }
    });
    useOutsideClick(item4Ref, () =&gt; {
      if (showPopup.item4) {
        setShowPopup({ ...showPopup, item4: false });
      }
    });
    useOutsideClick(item5Ref, () =&gt; {
      if (showPopup.item5) {
        setShowPopup({ ...showPopup, item5: false });
      }
    });
    useOutsideClick(item6Ref, () =&gt; {
      if (showPopup.item6) {
        setShowPopup({ ...showPopup, item6: false });
      }
    });
  
      return(
          &lt;ul&gt;
              &lt;li ref={item1Ref}&gt;
                  &lt;input type=&quot;checkbox&quot; id=&quot;item1editcheck&quot; name=&quot;item1editcheck&quot; onClick={(e) =&gt; {
                        setShowPopup({ ...showPopup, item1: true });
                      }} /&gt;
                  &lt;label htmlFor=&quot;item1editcheck&quot;&gt;Item 1&lt;/label&gt;
                  {showPopup.item1 &amp;&amp;(
                      &lt;div&gt;My content&lt;/div&gt;
                  )} 
              &lt;/li&gt;
              &lt;li ref={item2Ref}&gt;
                  &lt;input type=&quot;checkbox&quot; id=&quot;item2editcheck&quot; name=&quot;item2editcheck&quot; onClick={(e) =&gt; {
                        setShowPopup({ ...showPopup, item2: true });
                      }} /&gt;
                  &lt;label htmlFor=&quot;item2editcheck&quot;&gt;Item 2&lt;/label&gt;
                  {showPopup.item2 &amp;&amp;(
                      &lt;div&gt;My content&lt;/div&gt;
                  )} 
              &lt;/li&gt;
          &lt;/ul&gt;
      );
  
  }
  
  export default Dropdowns;
  </code></pre>
  <p>useOutsideClick.tsx</p>
  <pre><code>import { useEffect } from &quot;react&quot;;
  
  const useOutsideClick = (ref: any, callback: any) =&gt; {
    const handleClick = (e: any) =&gt; {
      if (ref.current &amp;&amp; !ref.current.contains(e.target)) {
        callback();
      }
    };
  
    useEffect(() =&gt; {
      document.addEventListener(&quot;click&quot;, handleClick);
  
      return () =&gt; {
        document.removeEventListener(&quot;click&quot;, handleClick);
      };
    });
  };
  
  export default useOutsideClick;
  </code></pre>