@ptolemy2002/react-mount-effects 1.2.1 → 2.0.0
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.md +12 -21
- package/dist/main.d.ts +4 -0
- package/dist/main.js +20 -0
- package/package.json +60 -35
- package/index.js +0 -42
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
React allows `useEffect(() => {callback()}, [])` to run the callback only on first mount, thus `useEffect(() => callback, [])` only runs on unmounting. However, eslint often complains about this. This library manually suppresses those warnings and exports the hooks for convenience sake.
|
|
3
3
|
|
|
4
4
|
The hooks are not exported as default, so you can import in one of the following ways:
|
|
5
|
-
```
|
|
5
|
+
```javascript
|
|
6
6
|
//ES6
|
|
7
7
|
import { useMountEffect, useUnmountEffect, useDelayedEffect } from '@ptolemy2002/react-mount-effects';
|
|
8
8
|
//CommonJS
|
|
@@ -19,7 +19,7 @@ The following hooks are available in the library:
|
|
|
19
19
|
Runs the callback only on first mount. The return value of the callback is ignored.
|
|
20
20
|
|
|
21
21
|
#### Parameters
|
|
22
|
-
- `callback` (
|
|
22
|
+
- `callback` (`() => void`): The callback to run on first mount.
|
|
23
23
|
|
|
24
24
|
#### Returns
|
|
25
25
|
None
|
|
@@ -29,7 +29,7 @@ None
|
|
|
29
29
|
Runs the callback only on unmount. The return value of the callback is ignored.
|
|
30
30
|
|
|
31
31
|
#### Parameters
|
|
32
|
-
- `callback` (
|
|
32
|
+
- `callback` (`() => void`): The callback to run on unmount.
|
|
33
33
|
|
|
34
34
|
#### Returns
|
|
35
35
|
None
|
|
@@ -39,34 +39,25 @@ None
|
|
|
39
39
|
Runs the callback only after it has been triggered `delay + 1` times (since the first is on mount and does not represent a state change). The return value of the callback is ignored.
|
|
40
40
|
|
|
41
41
|
#### Parameters
|
|
42
|
-
- `callback` (
|
|
43
|
-
- `deps` (
|
|
44
|
-
- `delay` (
|
|
42
|
+
- `callback` (`(changeCount: number, reset: () => void) => void`): The callback to run after the delay. The first parameter is the number of times the effect has been triggered since either initialization or last reset, and the second argument is a function that allows you to reset the counter.
|
|
43
|
+
- `deps` (`react.DependencyList`): An array of dependencies to listen to. This works exactly like the `useEffect` hook's dependencies. By default, this is an empty array.
|
|
44
|
+
- `delay` (`number`): The number of times the dependencies must change before the callback is run. Default is `0`, meaning the callback is run on any change, but not on mount.
|
|
45
45
|
|
|
46
46
|
#### Returns
|
|
47
47
|
None
|
|
48
48
|
|
|
49
|
-
## Meta
|
|
50
|
-
This is a React Library Created by Ptolemy2002's [cra-template-react-library](https://www.npmjs.com/package/@ptolemy2002/cra-template-react-library) template in combination with [create-react-app](https://www.npmjs.com/package/create-react-app). It contains methods of building and publishing your library to npm.
|
|
51
|
-
For now, the library makes use of React 18 and does not use TypeScript.
|
|
52
|
-
|
|
53
49
|
## Peer Dependencies
|
|
54
|
-
|
|
55
|
-
-
|
|
56
|
-
- @types/react-dom: ^18.3.0
|
|
57
|
-
- react: ^18.3.1
|
|
58
|
-
- react-dom: ^18.3.1
|
|
50
|
+
- `react^18.3.1`
|
|
51
|
+
- `react-dom^18.3.1`
|
|
59
52
|
|
|
60
53
|
## Commands
|
|
61
54
|
The following commands exist in the project:
|
|
62
55
|
|
|
63
|
-
- `npm run uninstall` - Uninstalls all dependencies for the library
|
|
64
|
-
- `npm run reinstall` - Uninstalls and then Reinstalls all dependencies for the library
|
|
65
|
-
- `npm run example-uninstall` - Uninstalls all dependencies for the example app
|
|
66
|
-
- `npm run example-install` - Installs all dependencies for the example app
|
|
67
|
-
- `npm run example-reinstall` - Uninstalls and then Reinstalls all dependencies for the example app
|
|
68
|
-
- `npm run example-start` - Starts the example app after building the library
|
|
69
56
|
- `npm run build` - Builds the library
|
|
57
|
+
- `npm run dev` - Starts the development server
|
|
58
|
+
- `npm run lint` - Lints the project
|
|
59
|
+
- `npm run uninstall` - Uninstalls all dependencies for the library and clears the cache
|
|
60
|
+
- `npm run reinstall` - Uninstalls, clears the cache, and then reinstalls all dependencies for the library
|
|
70
61
|
- `npm run release` - Publishes the library to npm without changing the version
|
|
71
62
|
- `npm run release-patch` - Publishes the library to npm with a patch version bump
|
|
72
63
|
- `npm run release-minor` - Publishes the library to npm with a minor version bump
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { DependencyList } from 'react';
|
|
2
|
+
export declare function useMountEffect(callback: () => void): void;
|
|
3
|
+
export declare function useUnmountEffect(callback: () => void): void;
|
|
4
|
+
export declare function useDelayedEffect(callback: (changeCount: number, reset: () => void) => void, deps?: DependencyList, delay?: number): void;
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useEffect as n, useRef as c } from "react";
|
|
2
|
+
function o(e) {
|
|
3
|
+
n(() => {
|
|
4
|
+
e();
|
|
5
|
+
}, []);
|
|
6
|
+
}
|
|
7
|
+
function s(e) {
|
|
8
|
+
n(() => e, []);
|
|
9
|
+
}
|
|
10
|
+
function i(e, u = [], f = 0) {
|
|
11
|
+
const t = c(0);
|
|
12
|
+
n(() => {
|
|
13
|
+
t.current++, t.current > f + 1 && e(t.current, () => t.current = 0);
|
|
14
|
+
}, u);
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
i as useDelayedEffect,
|
|
18
|
+
o as useMountEffect,
|
|
19
|
+
s as useUnmountEffect
|
|
20
|
+
};
|
package/package.json
CHANGED
|
@@ -1,38 +1,63 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
|
|
2
|
+
"name": "@ptolemy2002/react-mount-effects",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "2.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/Ptolemy2002/react-mount-effects"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"dev": "npm run build && vite --port 3000",
|
|
12
|
+
"build": "tsc -b ./tsconfig.lib.json && vite build",
|
|
13
|
+
"lint": "eslint .",
|
|
14
|
+
"preview": "vite preview",
|
|
15
|
+
"postinstall": "npx typesync",
|
|
16
|
+
"uninstall": "bash ./scripts/uninstall.sh",
|
|
17
|
+
"reinstall": "bash ./scripts/reinstall.sh",
|
|
18
|
+
"release": "bash ./scripts/release.sh",
|
|
19
|
+
"release-patch": "bash ./scripts/release.sh patch",
|
|
20
|
+
"release-minor": "bash ./scripts/release.sh minor",
|
|
21
|
+
"release-major": "bash ./scripts/release.sh major"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "^18.3.1",
|
|
25
|
+
"react-dom": "^18.3.1"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@eslint/js": "^9.11.1",
|
|
29
|
+
"@types/babel__generator": "^7.6.8",
|
|
30
|
+
"@types/babel__template": "^7.4.4",
|
|
31
|
+
"@types/babel__traverse": "^7.20.6",
|
|
32
|
+
"@types/eslint__js": "~8.42.3",
|
|
33
|
+
"@types/is-callable": "~1.1.2",
|
|
34
|
+
"@types/less": "~3.0.6",
|
|
35
|
+
"@types/node": "^22.7.4",
|
|
36
|
+
"@types/prop-types": "^15.7.13",
|
|
37
|
+
"@types/react": "^18.3.10",
|
|
38
|
+
"@types/react-dom": "^18.3.0",
|
|
39
|
+
"@vitejs/plugin-react": "^4.3.2",
|
|
40
|
+
"eslint": "^9.11.1",
|
|
41
|
+
"eslint-plugin-react": "^7.37.1",
|
|
42
|
+
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
|
|
43
|
+
"eslint-plugin-react-refresh": "^0.4.12",
|
|
44
|
+
"globals": "^15.9.0",
|
|
45
|
+
"less": "^4.2.0",
|
|
46
|
+
"path": "^0.12.7",
|
|
47
|
+
"react": "^18.3.1",
|
|
48
|
+
"react-dom": "^18.3.1",
|
|
49
|
+
"typescript": "^5.5.3",
|
|
50
|
+
"typescript-eslint": "^8.7.0",
|
|
51
|
+
"vite": "^5.4.8",
|
|
52
|
+
"vite-plugin-dts": "^4.2.3"
|
|
53
|
+
},
|
|
54
|
+
"exports": {
|
|
55
|
+
".": {
|
|
56
|
+
"types": "./dist/lib/main.d.ts",
|
|
57
|
+
"default": "./dist/main.js"
|
|
37
58
|
}
|
|
59
|
+
},
|
|
60
|
+
"files": [
|
|
61
|
+
"dist"
|
|
62
|
+
]
|
|
38
63
|
}
|
package/index.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// src/index.js
|
|
20
|
-
var src_exports = {};
|
|
21
|
-
__export(src_exports, {
|
|
22
|
-
useDelayedEffect: () => useDelayedEffect,
|
|
23
|
-
useMountEffect: () => useMountEffect,
|
|
24
|
-
useUnmountEffect: () => useUnmountEffect
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(src_exports);
|
|
27
|
-
var import_react = require("react");
|
|
28
|
-
function useMountEffect(callback) {
|
|
29
|
-
(0, import_react.useEffect)(() => {
|
|
30
|
-
callback();
|
|
31
|
-
}, []);
|
|
32
|
-
}
|
|
33
|
-
function useUnmountEffect(callback) {
|
|
34
|
-
(0, import_react.useEffect)(() => callback, []);
|
|
35
|
-
}
|
|
36
|
-
function useDelayedEffect(callback, deps = [], delay = 0) {
|
|
37
|
-
const changeCount = (0, import_react.useRef)(0);
|
|
38
|
-
(0, import_react.useEffect)(() => {
|
|
39
|
-
changeCount.current++;
|
|
40
|
-
if (changeCount.current > delay + 1) callback(changeCount, () => changeCount.current = 0);
|
|
41
|
-
}, deps);
|
|
42
|
-
}
|