source: imaps-frontend/node_modules/react-use/esm/useEvent.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import { useEffect } from 'react';
2import { isBrowser, off, on } from './misc/util';
3var defaultTarget = isBrowser ? window : null;
4var isListenerType1 = function (target) {
5 return !!target.addEventListener;
6};
7var isListenerType2 = function (target) {
8 return !!target.on;
9};
10var useEvent = function (name, handler, target, options) {
11 if (target === void 0) { target = defaultTarget; }
12 useEffect(function () {
13 if (!handler) {
14 return;
15 }
16 if (!target) {
17 return;
18 }
19 if (isListenerType1(target)) {
20 on(target, name, handler, options);
21 }
22 else if (isListenerType2(target)) {
23 target.on(name, handler, options);
24 }
25 return function () {
26 if (isListenerType1(target)) {
27 off(target, name, handler, options);
28 }
29 else if (isListenerType2(target)) {
30 target.off(name, handler, options);
31 }
32 };
33 }, [name, handler, target, JSON.stringify(options)]);
34};
35export default useEvent;
Note: See TracBrowser for help on using the repository browser.