Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

Handle click outside a component in react with hooks

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

/**
 * Hook that alerts clicks outside of the passed ref
 */
function useOutsideAlerter(ref) {
  useEffect(() => {
    /**
     * Alert if clicked on outside of element
     */
    function handleClickOutside(event) {
      if (ref.current && !ref.current.contains(event.target)) {
        alert("You clicked outside of me!");
      }
    }
    // Bind the event listener
    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      // Unbind the event listener on clean up
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, [ref]);
}

/**
 * Component that alerts if you click outside of it
 */
export default function OutsideAlerter(props) {
  const wrapperRef = useRef(null);
  useOutsideAlerter(wrapperRef);

  return <div ref={wrapperRef}>{props.children}</div>;
}
Comment

outside click hook react

import { useEffect, MutableRefObject } from 'react'

export const useOutsideClick = <T extends Array<MutableRefObject<any>>>(
  ref: T,
  callback: () => void
): void => {
  useEffect(() => {
    const handler = (event: MouseEvent): void => {
      // Check if the mouse click was within the element's ref.

      if (!ref || ref.length === 0) return
      const node = ref.find((x) => x?.current?.contains(event?.target as Node))

      if (!node) {
        callback()
      }
    }

    window.addEventListener('mousedown', handler)

    return (): void => {
      window.removeEventListener('mousedown', handler)
    }
  }, [ref, callback])
}

// Usage (it should be in the component*)
const firstRef  = useRef(null)
const secondRef = useRef(null)
const handleClick = () => { console.log('Clicked outside ref') }

useOutsideClick([firstRef, secondRef], handleClick)
Comment

click outside hook in react

import { useEffect } from 'react';

// Hook
function useOnClickOutside(ref, buttonRef, handler) {
  useEffect(
    () => {
      const listener = event => {
        // Do nothing if clicking ref's element or descendent elements
        if (!ref.current || ref.current.contains(event.target) || buttonRef.current.contains(event.target)) {
          return;
        }
        handler(event);
      };
      document.addEventListener('mousedown', listener);
      document.addEventListener('touchstart', listener);
      return () => {
        document.removeEventListener('mousedown', listener);
        document.removeEventListener('touchstart', listener);
      };
    },
    // Add ref and handler to effect dependencies
    // It's worth noting that because passed in handler is a new ...
    // ... function on every render that will cause this effect ...
    // ... callback/cleanup to run every render. It's not a big deal ...
    // ... but to optimize you can wrap handler in useCallback before ...
    // ... passing it into this hook.
    [ref, handler]
  );
}

export default useOnClickOutside;

//component/Header.tsx
const header = () => {
  const ref = useRef(null);
  const buttonRef = useRef(null);
  console.log(buttonRef);

  useOnClickOutside(ref, buttonRef, () => setIsOpen(false));
Comment

PREVIOUS NEXT
Code Example
Typescript :: Warning: call_user_func_array() expects parameter 1 to be a valid callback 
Typescript :: typescript date before 
Typescript :: tar: refusing to read archive contents from terminal (missing -f option?) tar: error is not recoverable: exiting now 
Typescript :: angular find and remove from string 
Typescript :: How can I call a method every x seconds? 
Typescript :: formgroup check if valid 
Typescript :: laravel validation exists multiple tables laravel 
Typescript :: typescript string concatenation best practice 
Typescript :: update object in array in ngxrx store in angular 
Typescript :: gettime is not a function typescript 
Typescript :: conventional commits cheat sheet 
Typescript :: how to read excel spreadsheets in c++ 
Typescript :: __REDUX_DEVTOOLS_EXTENSION_COMPOSE__ 
Typescript :: int sum. 
Typescript :: socket.io auth 
Typescript :: typescript readonly 
Typescript :: test coverage techniques 
Typescript :: split in angular 8 
Typescript :: share data across tab through localstorage 
Typescript :: angular minus date 
Typescript :: IM DEAD 
Typescript :: sum all elements using each_with_object ruby 
Typescript :: open url with pacage.json 
Typescript :: file attachements contac form 7 
Typescript :: python double check if wants to execute funtion 
Typescript :: java to typescript 
Typescript :: online doctor appointments in pakistan 
Typescript :: DISTINQUISH BETWEEN THE AVERAGE CASE AND WORSE CASE RUNNING TIME AND THE FACTORS AFFECTING THAT AFFECTS THE RUNNING TIME OF AN ALGORITHM 
Typescript :: exclude redults after theninclude 
Typescript :: after effects how to parent only one property 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =