Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

click outside React component

const componentRef = useRef();

useEffect(() => {
    document.addEventListener("click", handleClick);
    return () => document.removeEventListener("click", handleClick);
    function handleClick(e: any) {
        if(componentRef && componentRef.current){
            const ref: any = componentRef.current
            if(!ref.contains(e.target)){
                // put your action here
            }
        }
    }
}, []);
Comment

react click outside

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

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside class implementation

import React, { Component } from 'react';
import PropTypes from 'prop-types';

/**
 * Component that alerts if you click outside of it
 */
export default class OutsideAlerter extends Component {
    constructor(props) {
        super(props);

        this.wrapperRef = React.createRef();
        this.setWrapperRef = this.setWrapperRef.bind(this);
        this.handleClickOutside = this.handleClickOutside.bind(this);
    }

    componentDidMount() {
        document.addEventListener('mousedown', this.handleClickOutside);
    }

    componentWillUnmount() {
        document.removeEventListener('mousedown', this.handleClickOutside);
    }

    /**
     * Alert if clicked on outside of element
     */
    handleClickOutside(event) {
        if (this.wrapperRef && !this.wrapperRef.current.contains(event.target)) {
            alert('You clicked outside of me!');
        }
    }

    render() {
        return <div ref={this.wrapperRef}>{this.props.children}</div>;
    }
}

OutsideAlerter.propTypes = {
    children: PropTypes.element.isRequired,
};
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

outside click 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

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

react click outside

yarn add react-detect-click-outside
npm i react-detect-click-outside
Comment

PREVIOUS NEXT
Code Example
Javascript :: proper to mixed fraction in javascript 
Javascript :: show modal by using id in list react 
Javascript :: pdfjs get all the text present 
Javascript :: pdf.js extract text 
Javascript :: clone an object in javascript 
Javascript :: strict mode 
Javascript :: server side rendering 
Javascript :: circle progress bar react 
Javascript :: how to pick date from datepicker in selenium 
Javascript :: node fetch response body 
Javascript :: new date 
Javascript :: for...of Syntax 
Javascript :: angular local storage ionic 
Javascript :: what is morgan in nodejs 
Javascript :: (node:9130) electron: Failed to load URL: http://localhost:5000 
Javascript :: vue style 
Javascript :: javascript array destructuring last element 
Javascript :: trigger lambda function on s3 upload code 
Javascript :: setCenter: not a LatLng or LatLngLiteral with finite coordinates: in property lat: not a number 
Javascript :: async promise javascript 
Javascript :: Prisma where in array 
Javascript :: react router switch 
Javascript :: Ping discord 
Javascript :: get index of selector jquery 
Javascript :: randomize an array 
Javascript :: threejs perspectivecamera 
Javascript :: convert timestamp to date js 
Javascript :: next js get gurrent page params 
Javascript :: how to get firebase document id angular 
Javascript :: are you sure you want to close this window javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =