-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from trinhthinh388/master
Feat/use debounce (#106)
- Loading branch information
Showing
12 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ["@react-awesome/eslint-config/library.js"], | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
project: true, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# @react-awesome/use-debounce | ||
|
||
## 1.0.0 | ||
|
||
### Major Changes | ||
|
||
- Release first version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# @react-awesome/use-debounce | ||
|
||
<p align="center"> | ||
<img alt="version" src="https://img.shields.io/npm/v/%40react-awesome%2Fuse-debounce" /> | ||
<img alt="coverage" src="https://img.shields.io/codecov/c/github/trinhthinh388/react-awesome-components/master?token=VQ8VJ7OECQ&flag=use-debounce" /> | ||
<img alt="license" src="https://img.shields.io/github/license/trinhthinh388/react-awesome-components" /> | ||
<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/trinhthinh388/react-awesome-components/release.yml" /> | ||
<img alt="size" src="https://img.shields.io/bundlejs/size/%40react-awesome/use-debounce" /> | ||
</p> | ||
|
||
**use-debounce** tracks the previous value of a variable. | ||
|
||
Please refer to the [documentation](https://react-awesome-components.vercel.app/docs/use-debounce) for more information. | ||
|
||
## Installation | ||
|
||
```sh | ||
yarn add @react-awesome/use-debounce | ||
# or | ||
npm i @react-awesome/use-debounce | ||
``` | ||
|
||
## Contribution | ||
|
||
Yes please! See the | ||
[contributing guidelines](https://github.com/trinhthinh388/react-awesome-components/blob/master/CONTRIBUTING.md) | ||
for details. | ||
|
||
## Licence | ||
|
||
This project is licensed under the terms of the | ||
[MIT license](https://github.com/trinhthinh388/react-awesome-components/blob/master/LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "@react-awesome/use-debounce", | ||
"version": "1.0.0", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"sideEffects": false, | ||
"license": "MIT", | ||
"description": "useDebounce debounces the state value.", | ||
"keywords": [ | ||
"react", | ||
"debounce", | ||
"state", | ||
"react", | ||
"react", | ||
"hooks", | ||
"useDebounce" | ||
], | ||
"repository": { | ||
"url": "https://github.com/trinhthinh388/react-awesome-components" | ||
}, | ||
"files": [ | ||
"dist/**" | ||
], | ||
"scripts": { | ||
"build": "vite --config ../../vite.config.ts build", | ||
"dev": "vite --watch --config ../../vite.config.ts build", | ||
"lint": "eslint \"src/**/*.ts*\"", | ||
"clean": "rimraf .turbo && rimraf node_modules && rimraf dist", | ||
"test": "vitest --config ../../vite.config.ts --coverage --run", | ||
"test:ui": "vitest --config ../../vite.config.ts --coverage --ui", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"devDependencies": { | ||
"@react-awesome/eslint-config": "*", | ||
"@react-awesome/tsconfig": "*", | ||
"@types/react": "^18.2.46", | ||
"@types/react-dom": "^18.2.18", | ||
"eslint": "^8.56.0", | ||
"react": "^18.2.0", | ||
"typescript": "^5.3.3" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './use-debounce' | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useDebounce } from './use-debounce' | ||
import { renderHook } from '@testing-library/react' | ||
|
||
describe('useDebounce', () => { | ||
beforeEach(() => { | ||
// tell vitest we use mocked time | ||
vi.useFakeTimers() | ||
}) | ||
|
||
afterEach(() => { | ||
// restoring date after each test run | ||
vi.useRealTimers() | ||
}) | ||
|
||
it('Should debounce the state update', () => { | ||
const { result, rerender } = renderHook(() => useDebounce(1, 3_000)) | ||
expect(result.current).toBe(undefined) | ||
vi.runAllTimers() | ||
rerender() | ||
expect(result.current).toBe(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useEffect, useRef, useState } from 'react' | ||
|
||
export function useDebounce<T>(state: T, timeout: number = 3_00): T { | ||
const [debounced, setDebounced] = useState<T>() | ||
const timeoutId = useRef<number>() | ||
|
||
useEffect(() => { | ||
const id = timeoutId.current | ||
if (id) { | ||
clearTimeout(id) | ||
} | ||
|
||
timeoutId.current = window.setTimeout(() => { | ||
setDebounced(state) | ||
}, timeout) | ||
|
||
return () => { | ||
clearTimeout(id) | ||
} | ||
}, [state, timeout]) | ||
|
||
return debounced as T | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "@react-awesome/tsconfig/react-library.json", | ||
"compilerOptions": { | ||
"types": ["vitest/globals"], | ||
}, | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules"], | ||
} |