source: imaps-frontend/node_modules/react-use/lib/useEvent.js@ d565449

main
Last change on this file since d565449 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 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var react_1 = require("react");
4var util_1 = require("./misc/util");
5var defaultTarget = util_1.isBrowser ? window : null;
6var isListenerType1 = function (target) {
7 return !!target.addEventListener;
8};
9var isListenerType2 = function (target) {
10 return !!target.on;
11};
12var useEvent = function (name, handler, target, options) {
13 if (target === void 0) { target = defaultTarget; }
14 react_1.useEffect(function () {
15 if (!handler) {
16 return;
17 }
18 if (!target) {
19 return;
20 }
21 if (isListenerType1(target)) {
22 util_1.on(target, name, handler, options);
23 }
24 else if (isListenerType2(target)) {
25 target.on(name, handler, options);
26 }
27 return function () {
28 if (isListenerType1(target)) {
29 util_1.off(target, name, handler, options);
30 }
31 else if (isListenerType2(target)) {
32 target.off(name, handler, options);
33 }
34 };
35 }, [name, handler, target, JSON.stringify(options)]);
36};
37exports.default = useEvent;
Note: See TracBrowser for help on using the repository browser.