Languages
[Edit]
EN

React - detect click outside component

0 points
Created by:
Mariam-Barron
781

In this article, we would like to show you how to detect a click outside component in React.

 

Detect click outside component example
Detect click outside component example

Below are three sample codes, ready to run.

Example 1

// ONLINE-RUNNER:browser;

// Note: Uncomment import lines during working with JSX Compiler.
// import React from 'react';
// import ReactDOM from 'react-dom';

const App = () => {
  const [clickedOutside, setClickedOutside] = React.useState(false);
  const ref = React.useRef(null);

  React.useEffect(() => {
    const handleOutsideClick = e => {
      setClickedOutside(ref.current !== e.target);
    };
    window.addEventListener('click', handleOutsideClick);
    return () => window.removeEventListener('click', handleOutsideClick);
  }, []);

  return (
    <div >
      <button ref={ref}>
        {`clickedOutside: ${clickedOutside}`}
      </button>
    </div>
  );
};

const root = document.querySelector('#root');
ReactDOM.render(<App />, root);

Example 2

// ONLINE-RUNNER:browser;

// Note: Uncomment import lines during working with JSX Compiler.
// import React from "react";
// import ReactDOM from "react-dom";

const Component = ({ name, checked, onChange }) => {
  const ref = React.useRef();
  React.useEffect(() => {
    let clicked = false;
    const handleOutsideClick = () => {
      if (clicked) {
        clicked = false;
        return;
      }
      console.log('Outside click...');
    };
    const handleInsideClick = () => {
      clicked = true;
      console.log('Inside click...');
    };
    const component = ref.current;
    window.addEventListener('click', handleOutsideClick)
    if (component) {
      component.addEventListener('click', handleInsideClick);
    }
    return () => {
      window.addEventListener('click', handleOutsideClick);
      if (component) {
        component.addEventListener('click', handleInsideClick);
      }
    };
  }, []);
  return (
    <div ref={ref} style={{ background: 'orange' }}>
      Component body...
    </div>
  );
};

const App = () => {
  return (
    <Component />
  );
};

const root = document.querySelector('#root');
ReactDOM.render(<App />, root);

Example 3

// ONLINE-RUNNER:browser;

// Note: Uncomment import lines during working with JSX Compiler.
// import React from 'react';
// import ReactDOM from 'react-dom';

const useHandleOutsideClick = (onOutsideClick, onInsideClick) => {
  const ref = React.useRef();
  React.useEffect(() => {
    let clicked = false;
    const handleOutsideClick = (e) => {
      if (clicked) {
        clicked = false;
        return;
      }
      if (onOutsideClick) {
        onOutsideClick(e);
      }
    };
    const handleInsideClick = (e) => {
      clicked = true;
      if (onInsideClick) {
        onInsideClick(e);
      }
    };
    const component = ref.current;
    window.addEventListener('click', handleOutsideClick)
    if (component) {
      component.addEventListener('click', handleInsideClick);
    }
    return () => {
      window.addEventListener('click', handleOutsideClick);
      if (component) {
        component.addEventListener('click', handleInsideClick);
      }
    };
  }, []);
  return ref;
};

// Usage example:

const Component = ({ name, checked, onChange }) => {
  const handleOutsideClick = () => {
    console.log('Outside click...');
  };
  const handleInsideClick = () => {
    console.log('Inside click...');
  };
  const ref = useHandleOutsideClick(handleOutsideClick, handleInsideClick);
  return (
    <div ref={ref} style={{ background: 'orange' }}>
      Component body...
    </div>
  );
};

const App = () => {
  return (
    <Component />
  );
};

const root = document.querySelector('#root');
ReactDOM.render(<App />, root);

See also

  1. React - detect click outside component hook (works with nested elements)

References

  1. addEventListener - MDN
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS

React - detect click outside component
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join