Skip to content

Commit 485b731

Browse files
hooks
0 parents  commit 485b731

File tree

11 files changed

+6866
-0
lines changed

11 files changed

+6866
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 aldrinpvincent
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# react-use-custom-hooks
2+
3+
A collection of react custom hooks
4+
5+
# Installation
6+
7+
```bash
8+
npm install --save react-use-custom-hooks
9+
yarn add react-use-custom-hooks
10+
```
11+
12+
# Usage
13+
14+
```ts
15+
import { useSafeState } from 'react-use-custom-hooks';
16+
```
17+
18+
# Hooks list
19+
20+
1. useDebounce
21+
2. useSafeState
22+
3. useIsMounted
23+
4. usePrevious

package.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"version": "0.1.3",
3+
"license": "MIT",
4+
"main": "dist/index.js",
5+
"typings": "dist/index.d.ts",
6+
"files": [
7+
"dist",
8+
"src"
9+
],
10+
"engines": {
11+
"node": ">=10"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/aldrinpvincent/react-use-custom-hooks"
16+
},
17+
"scripts": {
18+
"start": "tsdx watch",
19+
"build": "tsdx build",
20+
"test": "tsdx test --passWithNoTests",
21+
"lint": "tsdx lint",
22+
"prepare": "tsdx build",
23+
"size": "size-limit",
24+
"analyze": "size-limit --why"
25+
},
26+
"peerDependencies": {
27+
"react": ">=16"
28+
},
29+
"husky": {
30+
"hooks": {
31+
"pre-commit": "tsdx lint"
32+
}
33+
},
34+
"prettier": {
35+
"printWidth": 80,
36+
"semi": true,
37+
"singleQuote": true,
38+
"trailingComma": "es5"
39+
},
40+
"name": "react-use-custom-hooks",
41+
"author": "aldrinpvincent",
42+
"module": "dist/react-custom-hooks.esm.js",
43+
"description": "A collection of react custom hooks in typescript",
44+
"size-limit": [
45+
{
46+
"path": "dist/react-custom-hooks.cjs.production.min.js",
47+
"limit": "10 KB"
48+
},
49+
{
50+
"path": "dist/react-custom-hooks.esm.js",
51+
"limit": "10 KB"
52+
}
53+
],
54+
"devDependencies": {
55+
"@size-limit/preset-small-lib": "^7.0.8",
56+
"@types/react": "^17.0.39",
57+
"@types/react-dom": "^17.0.11",
58+
"husky": "^7.0.4",
59+
"react": "^17.0.2",
60+
"react-dom": "^17.0.2",
61+
"size-limit": "^7.0.8",
62+
"tsdx": "^0.14.1",
63+
"tslib": "^2.3.1",
64+
"typescript": "^4.5.5"
65+
},
66+
"keywords": [
67+
"react custom hooks",
68+
"typescript",
69+
"memory leak"
70+
]
71+
}

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { useDebounce } from './useDebounce';
2+
import { useSafeState } from './useSafeState';
3+
import { useIsMounted } from './useIsMounted';
4+
import { usePrevious } from './usePrevious';
5+
import { useLegacyState } from './useLegacyState';
6+
7+
export { useDebounce, useSafeState, useIsMounted, usePrevious, useLegacyState };

src/useDebounce.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useEffect, useState } from 'react';
2+
3+
export function useDebounce<T>(value: T, delay: number = 500) {
4+
const [debouncedValue, setDebouncedValue] = useState(value);
5+
6+
useEffect(() => {
7+
const handler = setTimeout(() => {
8+
setDebouncedValue(value);
9+
}, delay);
10+
11+
return () => clearTimeout(handler);
12+
}, [delay, value]);
13+
14+
return debouncedValue;
15+
}

src/useIsMounted.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useCallback, useEffect, useRef } from 'react';
2+
3+
export function useIsMounted() {
4+
const mountedRef = useRef(false);
5+
const isMounted = useCallback(() => mountedRef.current, []);
6+
7+
useEffect(() => {
8+
mountedRef.current = true;
9+
return () => {
10+
mountedRef.current = false;
11+
};
12+
}, []);
13+
14+
return isMounted;
15+
}

src/useLegacyState.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useReducer } from 'react';
2+
3+
export function useLegacyState<T>(
4+
initialState: T
5+
): [T, (value: Record<string, any>) => void] {
6+
return useReducer(
7+
(state: T, update: Record<string, any>) => ({
8+
...state,
9+
...update,
10+
}),
11+
initialState
12+
);
13+
}

src/usePrevious.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useEffect, useRef } from 'react';
2+
3+
export function usePrevious<T>(value: T): T {
4+
// Store current value in ref
5+
const ref: any = useRef<T>();
6+
7+
useEffect(() => {
8+
// Update the value if it changes
9+
ref.current = value;
10+
}, [value]);
11+
12+
// return the previous value
13+
return ref.current;
14+
}

src/useSafeState.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useCallback, useState } from 'react';
2+
import { useIsMounted } from './useIsMounted';
3+
4+
export function useSafeState<T>(initialState: T): [T, (value: T) => void] {
5+
const isMounted = useIsMounted();
6+
const [state, setState] = useState(initialState);
7+
8+
const safeSetState = useCallback(
9+
(value: T) => {
10+
if (isMounted()) {
11+
setState(value);
12+
}
13+
},
14+
[isMounted]
15+
);
16+
17+
return [state, safeSetState];
18+
}

tsconfig.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
// see https://www.typescriptlang.org/tsconfig to better understand tsconfigs
3+
"include": ["src", "types"],
4+
"compilerOptions": {
5+
"module": "esnext",
6+
"lib": ["dom", "esnext"],
7+
"importHelpers": true,
8+
// output .d.ts declaration files for consumers
9+
"declaration": true,
10+
// output .js.map sourcemap files for consumers
11+
"sourceMap": true,
12+
// match output dir to input dir. e.g. dist/index instead of dist/src/index
13+
"rootDir": "./src",
14+
// stricter type-checking for stronger correctness. Recommended by TS
15+
"strict": true,
16+
// linter checks for common issues
17+
"noImplicitReturns": true,
18+
"noFallthroughCasesInSwitch": true,
19+
// noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative
20+
"noUnusedLocals": true,
21+
"noUnusedParameters": true,
22+
// use Node's module resolution algorithm, instead of the legacy TS one
23+
"moduleResolution": "node",
24+
// transpile JSX to React.createElement
25+
"jsx": "react",
26+
// interop between ESM and CJS modules. Recommended by TS
27+
"esModuleInterop": true,
28+
// significant perf increase by skipping checking .d.ts files, particularly those in node_modules. Recommended by TS
29+
"skipLibCheck": true,
30+
// error out if import and file system have a casing mismatch. Recommended by TS
31+
"forceConsistentCasingInFileNames": true,
32+
// `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc`
33+
"noEmit": true,
34+
}
35+
}

0 commit comments

Comments
 (0)