@monkvision/common 4.3.11 → 4.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README/HOOKS.md +20 -0
- package/lib/apps/appStateProvider.js +4 -2
- package/lib/hooks/useIsMounted.d.ts +5 -0
- package/lib/hooks/useIsMounted.js +19 -0
- package/package.json +13 -13
package/README/HOOKS.md
CHANGED
|
@@ -83,6 +83,26 @@ function TestComponent() {
|
|
|
83
83
|
This custom hook creates an interval that calls the provided callback every `delay` milliseconds. If `delay` is `null`
|
|
84
84
|
or less than 0, the callback will not be called.
|
|
85
85
|
|
|
86
|
+
### useIsMounted
|
|
87
|
+
```tsx
|
|
88
|
+
import { useIsMounted } from '@monkvision/common';
|
|
89
|
+
|
|
90
|
+
function TestComponent() {
|
|
91
|
+
const [example, setExample] = useState(0);
|
|
92
|
+
const isMounted = useIsMounted();
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
myAsyncFunc().then((value) => {
|
|
96
|
+
if (isMounted()) {
|
|
97
|
+
setExample(value);
|
|
98
|
+
}
|
|
99
|
+
}).catch(console.error);
|
|
100
|
+
}, [isMounted]);
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
Custom hook returning a ref to a util function returning `true` if the component using the hook is mounted, and false
|
|
104
|
+
otherwise. Can be used to cancel asynchronous calls on component unmounts.
|
|
105
|
+
|
|
86
106
|
### useForm
|
|
87
107
|
|
|
88
108
|
```tsx
|
|
@@ -21,6 +21,7 @@ var appState_1 = require("./appState");
|
|
|
21
21
|
var monitoring_1 = require("./monitoring");
|
|
22
22
|
var analytics_1 = require("./analytics");
|
|
23
23
|
var utils_1 = require("../utils");
|
|
24
|
+
var useIsMounted_1 = require("../hooks/useIsMounted");
|
|
24
25
|
/**
|
|
25
26
|
* Local storage key used within Monk web applications to store the authentication token.
|
|
26
27
|
*/
|
|
@@ -63,6 +64,7 @@ function MonkAppStateProvider(_a) {
|
|
|
63
64
|
var _e = (0, react_1.useState)(null), steeringWheel = _e[0], setSteeringWheel = _e[1];
|
|
64
65
|
var availableVehicleTypes = (0, react_1.useMemo)(function () { return (0, utils_1.getAvailableVehicleTypes)(config); }, [config]);
|
|
65
66
|
var monkSearchParams = (0, searchParams_1.useMonkSearchParams)({ availableVehicleTypes: availableVehicleTypes });
|
|
67
|
+
var isMounted = (0, useIsMounted_1.useIsMounted)();
|
|
66
68
|
(0, monitoring_1.useAppStateMonitoring)({ authToken: authToken, inspectionId: inspectionId, vehicleType: vehicleType, steeringWheel: steeringWheel });
|
|
67
69
|
(0, analytics_1.useAppStateAnalytics)({ inspectionId: inspectionId });
|
|
68
70
|
(0, react_1.useEffect)(function () {
|
|
@@ -75,11 +77,11 @@ function MonkAppStateProvider(_a) {
|
|
|
75
77
|
setVehicleType(function (param) { var _a; return (_a = monkSearchParams.get(searchParams_1.MonkSearchParam.VEHICLE_TYPE)) !== null && _a !== void 0 ? _a : param; });
|
|
76
78
|
setSteeringWheel(function (param) { var _a; return (_a = monkSearchParams.get(searchParams_1.MonkSearchParam.STEERING_WHEEL)) !== null && _a !== void 0 ? _a : param; });
|
|
77
79
|
var lang = monkSearchParams.get(searchParams_1.MonkSearchParam.LANGUAGE);
|
|
78
|
-
if (lang) {
|
|
80
|
+
if (lang && isMounted()) {
|
|
79
81
|
onFetchLanguage === null || onFetchLanguage === void 0 ? void 0 : onFetchLanguage(lang);
|
|
80
82
|
}
|
|
81
83
|
}
|
|
82
|
-
if (fetchedToken) {
|
|
84
|
+
if (fetchedToken && isMounted()) {
|
|
83
85
|
setAuthToken(fetchedToken);
|
|
84
86
|
onFetchAuthToken === null || onFetchAuthToken === void 0 ? void 0 : onFetchAuthToken();
|
|
85
87
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useIsMounted = void 0;
|
|
4
|
+
var react_1 = require("react");
|
|
5
|
+
/**
|
|
6
|
+
* Custom hook returning a ref to a util function returning `true` if the component using the hook is mounted, and false
|
|
7
|
+
* otherwise.
|
|
8
|
+
*/
|
|
9
|
+
function useIsMounted() {
|
|
10
|
+
var isMounted = (0, react_1.useRef)(false);
|
|
11
|
+
(0, react_1.useEffect)(function () {
|
|
12
|
+
isMounted.current = true;
|
|
13
|
+
return function () {
|
|
14
|
+
isMounted.current = false;
|
|
15
|
+
};
|
|
16
|
+
}, []);
|
|
17
|
+
return (0, react_1.useCallback)(function () { return isMounted.current; }, []);
|
|
18
|
+
}
|
|
19
|
+
exports.useIsMounted = useIsMounted;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@monkvision/common",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.13",
|
|
4
4
|
"license": "BSD-3-Clause-Clear",
|
|
5
5
|
"packageManager": "yarn@3.2.4",
|
|
6
6
|
"description": "MonkJs common logic package",
|
|
@@ -28,10 +28,10 @@
|
|
|
28
28
|
"lint:fix": "yarn run prettier:fix && yarn run eslint:fix"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@monkvision/analytics": "4.3.
|
|
32
|
-
"@monkvision/monitoring": "4.3.
|
|
33
|
-
"@monkvision/sights": "4.3.
|
|
34
|
-
"@monkvision/types": "4.3.
|
|
31
|
+
"@monkvision/analytics": "4.3.13",
|
|
32
|
+
"@monkvision/monitoring": "4.3.13",
|
|
33
|
+
"@monkvision/sights": "4.3.13",
|
|
34
|
+
"@monkvision/types": "4.3.13",
|
|
35
35
|
"i18next": "^23.4.5",
|
|
36
36
|
"jsonwebtoken": "^9.0.2",
|
|
37
37
|
"jwt-decode": "^4.0.0",
|
|
@@ -47,13 +47,13 @@
|
|
|
47
47
|
"react-router-dom": "^6.22.3"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@monkvision/eslint-config-base": "4.3.
|
|
51
|
-
"@monkvision/eslint-config-typescript": "4.3.
|
|
52
|
-
"@monkvision/eslint-config-typescript-react": "4.3.
|
|
53
|
-
"@monkvision/jest-config": "4.3.
|
|
54
|
-
"@monkvision/prettier-config": "4.3.
|
|
55
|
-
"@monkvision/test-utils": "4.3.
|
|
56
|
-
"@monkvision/typescript-config": "4.3.
|
|
50
|
+
"@monkvision/eslint-config-base": "4.3.13",
|
|
51
|
+
"@monkvision/eslint-config-typescript": "4.3.13",
|
|
52
|
+
"@monkvision/eslint-config-typescript-react": "4.3.13",
|
|
53
|
+
"@monkvision/jest-config": "4.3.13",
|
|
54
|
+
"@monkvision/prettier-config": "4.3.13",
|
|
55
|
+
"@monkvision/test-utils": "4.3.13",
|
|
56
|
+
"@monkvision/typescript-config": "4.3.13",
|
|
57
57
|
"@testing-library/react": "^12.1.5",
|
|
58
58
|
"@testing-library/react-hooks": "^8.0.1",
|
|
59
59
|
"@types/jest": "^29.2.2",
|
|
@@ -96,5 +96,5 @@
|
|
|
96
96
|
"url": "https://github.com/monkvision/monkjs/issues"
|
|
97
97
|
},
|
|
98
98
|
"homepage": "https://github.com/monkvision/monkjs",
|
|
99
|
-
"gitHead": "
|
|
99
|
+
"gitHead": "01b426413fda27038e5ecbcb74037f2d077a97c6"
|
|
100
100
|
}
|