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

Below are three sample codes, ready to run.
xxxxxxxxxx
1
// Note: Uncomment import lines during working with JSX Compiler.
2
// import React from 'react';
3
// import ReactDOM from 'react-dom';
4
5
const App = () => {
6
const [clickedOutside, setClickedOutside] = React.useState(false);
7
const ref = React.useRef(null);
8
9
React.useEffect(() => {
10
const handleOutsideClick = e => {
11
setClickedOutside(ref.current !== e.target);
12
};
13
window.addEventListener('click', handleOutsideClick);
14
return () => window.removeEventListener('click', handleOutsideClick);
15
}, []);
16
17
return (
18
<div >
19
<button ref={ref}>
20
{`clickedOutside: ${clickedOutside}`}
21
</button>
22
</div>
23
);
24
};
25
26
const root = document.querySelector('#root');
27
ReactDOM.render(<App />, root);
xxxxxxxxxx
1
// Note: Uncomment import lines during working with JSX Compiler.
2
// import React from "react";
3
// import ReactDOM from "react-dom";
4
5
const Component = ({ name, checked, onChange }) => {
6
const ref = React.useRef();
7
React.useEffect(() => {
8
let clicked = false;
9
const handleOutsideClick = () => {
10
if (clicked) {
11
clicked = false;
12
return;
13
}
14
console.log('Outside click...');
15
};
16
const handleInsideClick = () => {
17
clicked = true;
18
console.log('Inside click...');
19
};
20
const component = ref.current;
21
window.addEventListener('click', handleOutsideClick)
22
if (component) {
23
component.addEventListener('click', handleInsideClick);
24
}
25
return () => {
26
window.addEventListener('click', handleOutsideClick);
27
if (component) {
28
component.addEventListener('click', handleInsideClick);
29
}
30
};
31
}, []);
32
return (
33
<div ref={ref} style={{ background: 'orange' }}>
34
Component body...
35
</div>
36
);
37
};
38
39
const App = () => {
40
return (
41
<Component />
42
);
43
};
44
45
const root = document.querySelector('#root');
46
ReactDOM.render(<App />, root);
xxxxxxxxxx
1
// Note: Uncomment import lines during working with JSX Compiler.
2
// import React from 'react';
3
// import ReactDOM from 'react-dom';
4
5
const useHandleOutsideClick = (onOutsideClick, onInsideClick) => {
6
const ref = React.useRef();
7
React.useEffect(() => {
8
let clicked = false;
9
const handleOutsideClick = (e) => {
10
if (clicked) {
11
clicked = false;
12
return;
13
}
14
if (onOutsideClick) {
15
onOutsideClick(e);
16
}
17
};
18
const handleInsideClick = (e) => {
19
clicked = true;
20
if (onInsideClick) {
21
onInsideClick(e);
22
}
23
};
24
const component = ref.current;
25
window.addEventListener('click', handleOutsideClick)
26
if (component) {
27
component.addEventListener('click', handleInsideClick);
28
}
29
return () => {
30
window.addEventListener('click', handleOutsideClick);
31
if (component) {
32
component.addEventListener('click', handleInsideClick);
33
}
34
};
35
}, []);
36
return ref;
37
};
38
39
// Usage example:
40
41
const Component = ({ name, checked, onChange }) => {
42
const handleOutsideClick = () => {
43
console.log('Outside click...');
44
};
45
const handleInsideClick = () => {
46
console.log('Inside click...');
47
};
48
const ref = useHandleOutsideClick(handleOutsideClick, handleInsideClick);
49
return (
50
<div ref={ref} style={{ background: 'orange' }}>
51
Component body...
52
</div>
53
);
54
};
55
56
const App = () => {
57
return (
58
<Component />
59
);
60
};
61
62
const root = document.querySelector('#root');
63
ReactDOM.render(<App />, root);