Skip to content

Commit

Permalink
Merge pull request #96 from heyjul3s/feat/useDebounce
Browse files Browse the repository at this point in the history
Feat/use debounce
  • Loading branch information
heyjul3s committed Mar 4, 2021
2 parents d64e607 + e3838f1 commit 24598a9
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 2 deletions.
80 changes: 80 additions & 0 deletions packages/usedebouncedfn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# `@artifak/usedebouncefn`

A react hook to debounce functions/callbacks.

## Installation

### Yarn

```sh
yarn add @artifak/usedebouncefn
```

### NPM

```sh
npm install @artifak/usedebouncefn
```

## Usage

```ts
import React from 'react';
import { useDebouncedFn } from 'artifak';

export function Debounce() {
const [searchString, setSearchString] = React.useState('');
const [dropDownOptions, setDropdownOptions] = React.useState([]);

const getDropDown = async val => {
await fetch('http://jsonplaceholder.typicode.com/posts')
.then(res => {
if (res.ok) {
return res.json();
}

throw res;
})
.then(res => {
const data = res.filter(datum => {
return datum.title.includes(val);
});

setDropdownOptions(data);
})
.catch(res => {
console.error(res);
});
};

const debounceDropDown = useDebouncedFn(
nextValue => getDropDown(nextValue),
1000
);

const onInputChangeHandler = ev => {
const nextValue = ev.target.value;
setSearchString(nextValue);
debounceDropDown(nextValue);
};

return (
<div>
<input
className="input"
placeholder="Type Something..."
onChange={onInputChangeHandler}
value={searchString}
/>

{dropDownOptions.length && (
<div className="options">
{dropDownOptions.map(dropDownOption => (
<div key={dropDownOption.id}>{dropDownOption.title}</div>
))}
</div>
)}
</div>
);
}
```
10 changes: 10 additions & 0 deletions packages/usedebouncedfn/__tests__/useDebounceFn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { renderHook } from '@testing-library/react-hooks';
import { useDebouncedFn } from '../src';

describe('@artifak/usedebouncefn', () => {
test('should match media query', () => {
const testFn = () => console.log('Hello World');
const { result } = renderHook(() => useDebouncedFn(testFn, 300));
expect(result.current).toBeDefined();
});
});
48 changes: 48 additions & 0 deletions packages/usedebouncedfn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@artifak/usedebouncedfn",
"version": "1.0.0",
"description": "A React hook to debounce functions or callbacks.",
"keywords": [
"react",
"debounce",
"hooks"
],
"author": "Julian Low",
"license": "MIT",
"sideEffects": false,
"main": "dist/usedebouncedfn.cjs.js",
"module": "dist/usedebouncedfn.esm.js",
"src": "src/index.ts",
"types": "dist/src/index.d.ts",
"directories": {
"src": "src",
"test": "__tests__"
},
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/heyjul3s/artifak.git"
},
"scripts": {
"build": "rimraf dist && builder",
"test": "echo \"Error: run tests from root\" && exit 1"
},
"bugs": {
"url": "https://github.com/heyjul3s/artifak/issues"
},
"homepage": "https://github.com/heyjul3s/artifak#readme",
"devDependencies": {
"@artifak/bundler": "^1.1.4",
"@types/lodash.debounce": "^4.0.6"
},
"dependencies": {
"lodash.debounce": "^4.0.8",
"react-async-hook": "^3.6.2",
"use-constant": "^1.1.0"
}
}
20 changes: 20 additions & 0 deletions packages/usedebouncedfn/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useRef, useCallback, useEffect } from 'react';
import debounce from 'lodash.debounce';

export function useDebouncedFn<
Fn extends (...args: any[]) => any,
Delay extends number
>(callback: Fn, delay: Delay) {
const memoizedCallback = useCallback<Fn>(callback, []);
const debouncedFn = useRef(debounce(memoizedCallback, delay));

useEffect(() => {
debouncedFn.current = debounce(memoizedCallback, delay);

return function unmountUseDebounce() {
debouncedFn.current.cancel();
};
}, [debouncedFn, delay]);

return debouncedFn.current;
}
9 changes: 9 additions & 0 deletions packages/usedebouncedfn/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "."
},
"references": [],
"include": ["src"]
}
26 changes: 24 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3481,6 +3481,18 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
integrity sha512-cxWFQVseBm6O9Gbw1IWb8r6OS4OhSt3hPZLkFApLjM8TEXROBuQGLAH2i2gZpcXdLBIrpXuTDhH7Vbm1iXmNGA==

"@types/lodash.debounce@^4.0.6":
version "4.0.6"
resolved "https://registry.yarnpkg.com/@types/lodash.debounce/-/lodash.debounce-4.0.6.tgz#c5a2326cd3efc46566c47e4c0aa248dc0ee57d60"
integrity sha512-4WTmnnhCfDvvuLMaF3KV4Qfki93KebocUF45msxhYyjMttZDQYzHkO639ohhk8+oco2cluAFL3t5+Jn4mleylQ==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.168"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008"
integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==

"@types/markdown-to-jsx@^6.11.0":
version "6.11.3"
resolved "https://registry.yarnpkg.com/@types/markdown-to-jsx/-/markdown-to-jsx-6.11.3.tgz#cdd1619308fecbc8be7e6a26f3751260249b020e"
Expand Down Expand Up @@ -12154,6 +12166,11 @@ raw-loader@^4.0.1:
loader-utils "^2.0.0"
schema-utils "^3.0.0"

react-async-hook@^3.6.2:
version "3.6.2"
resolved "https://registry.yarnpkg.com/react-async-hook/-/react-async-hook-3.6.2.tgz#360018e5cd6ecc8841152a79875841b9e49dbdba"
integrity sha512-RkwHCJ8V7I6umKZLHneapuTRWf+eO4LOj0qUwUDsSn27jrAOcW6ClbV3x22Z4hVxH9bA0zb7y+ozDJDJ8PnZoA==

react-color@^2.17.0:
version "2.19.3"
resolved "https://registry.yarnpkg.com/react-color/-/react-color-2.19.3.tgz#ec6c6b4568312a3c6a18420ab0472e146aa5683d"
Expand Down Expand Up @@ -12227,7 +12244,7 @@ react-docgen@^5.0.0:
node-dir "^0.1.10"
strip-indent "^3.0.0"

react-dom@>=17.0.1, react-dom@^17.0.1:
react-dom@^17.0.1:
version "17.0.1"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-17.0.1.tgz#1de2560474ec9f0e334285662ede52dbc5426fc6"
integrity sha512-6eV150oJZ9U2t9svnsspTMrWNyHc6chX0KzDeAOXftRa8bNeOKTTfCJ7KorIwenkHd2xqVTBTCZd79yk/lx/Ug==
Expand Down Expand Up @@ -12381,7 +12398,7 @@ react-textarea-autosize@^8.1.1:
use-composed-ref "^1.0.0"
use-latest "^1.0.0"

react@>=17.0.1, react@^17.0.1:
react@^17.0.1:
version "17.0.1"
resolved "https://registry.yarnpkg.com/react/-/react-17.0.1.tgz#6e0600416bd57574e3f86d92edba3d9008726127"
integrity sha512-lG9c9UuMHdcAexXtigOZLX8exLWkW0Ku29qPRU8uhF2R9BN96dLCt0psvzPLlHc5OWkgymP3qwTRgbnw5BKx3w==
Expand Down Expand Up @@ -14636,6 +14653,11 @@ use-composed-ref@^1.0.0:
dependencies:
ts-essentials "^2.0.3"

use-constant@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/use-constant/-/use-constant-1.1.0.tgz#76d36a0edf16d4cc8565361f522b55da5f8f3f22"
integrity sha512-yrflEfv7Xv/W8WlYV6nwRH01K+2BpR4cWxuzY03yPRjYZuHixhGlvnJN5O2bRYrXGpJ4zy8QjFABGIQ2QXeBOA==

use-isomorphic-layout-effect@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/use-isomorphic-layout-effect/-/use-isomorphic-layout-effect-1.1.1.tgz#7bb6589170cd2987a152042f9084f9effb75c225"
Expand Down

0 comments on commit 24598a9

Please sign in to comment.