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

174
Vistas
How do I check if a specific element or it's children have been/not been clicked with react.js

I'm trying to check if anything but the ordered list and it's children have been clicked here

function Heeeeelp😭() {
    function checkClick(e) {
        if (!e.target == first-list || children) { //looking for something like this
            //do something
        }
    }
    return (
        <div onClick={e => {checkClick(e)}} className="container">
            <ol className="first-list">
                <li></li>
                <li></li>
                <li></li>
            </ol>
            <ul className="second-list">
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
)}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use something like this if (e.target.closest('.second-list')) {..}, this statement, here is a complete example

const { useState } = React;

function Comp() {
    const [ clicked, setClicked ] = useState('NONE');
    function checkClick(e) {
        if (e.target.closest('.first-list')) {
          setClicked('first list');
        }
        
        if (e.target.closest('.second-list')) {
          setClicked('second list');
        }
    }
    return (
        <div onClick={e => {checkClick(e)}} className="container">
            <ol className="first-list">
                <li>first</li>
                <li>second</li>
                <li>third</li>
            </ol>
            <ul className="second-list">
                <li>first</li>
                <li>second</li>
                <li>third</li>
            </ul>
            <div>Clicked List <strong>{ clicked }</strong></div>
        </div>
)}

ReactDOM.render(
  <Comp/>,
  document.body
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

I've used this pattern a bunch and it does the trick (assuming you can use hooks). An equivalent pattern could be made without hooks if needed.

import { useRef, useEffect } from "react";

export default function App() {
  const listRef = useRef(null);

  const handleClickOutside = (e) => {
    if (listRef.current && !listRef.current.contains(e.target)) {
      console.log("Clicked somewhere besides the list.");
    }
  };

  useEffect(() => {
    document.addEventListener("mousedown", handleClickOutside, true);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside, true);
    };
  }, []);

  return (
    <div>
      <ol ref={listRef}>
        <li>List 0 Item 0</li>
        <li>List 0 Item 1</li>
        <li>List 0 Item 2</li>
      </ol>
      <ul>
        <li>List 1 Item 0</li>
        <li>List 1 Item 1</li>
        <li>List 1 Item 2</li>
      </ul>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can write 2 separate functions, one for each list and pass them directly to the lists instead of trying to handle everything from the container.

That way, clicking on any child of a list will trigger the correct desired behavior

const Component = () => {
  const handleClickFirstList = () => {
    // do something
  }

  const handleClickSecondList = () => {
    // do something else
  }

  return (
    <div className="container">
      <ol className="first-list" onClick={handleClickFirstList}>
        <li></li>
        <li></li>
        <li></li>
      </ol>
      <ul className="second-list" onClick={handleClickSecondList}>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>
)}
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