Skip to content

Commit 1a16dc9

Browse files
committed
init
0 parents  commit 1a16dc9

8 files changed

+2284
-0
lines changed

.babelrc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"presets": [
3+
[
4+
"env",
5+
{
6+
"targets": {
7+
"browsers": ["last 2 versions", "> 1%"]
8+
}
9+
}
10+
]
11+
],
12+
"plugins": [["transform-object-rest-spread"]]
13+
}

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.idea
2+
.vscode
3+
examples/bundle.js
4+
node_modules
5+
npm-debug.js
6+
lib
7+
.git
8+
.DS_store

.prettierrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"printWidth": 120,
3+
"tabWidth": 2,
4+
"singleQuote": true,
5+
"trailingComma": "es5",
6+
"arrowParens": "always"
7+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Bhaskar Gyan Vardhan
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

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# useDebouncedEffect react hook
2+
3+
Install it with yarn:
4+
5+
```
6+
yarn add use-throttled-effect --save
7+
```
8+
9+
Or with npm:
10+
11+
```
12+
npm i use-throttled-effect --save
13+
```
14+
15+
#Example
16+
```javascript
17+
import React, { useState } from 'react';
18+
import useThrottledEffect from 'use-throttled-effect';
19+
20+
export default function Input() {
21+
const [count, setCount] = useState(0);
22+
23+
useEffect(()=>{
24+
const interval = setInterval(() => setCount(count=>count+1) ,100);
25+
return ()=>clearInterval(interval);
26+
},[])
27+
28+
useThrottledEffect(()=>{
29+
console.log(count);
30+
}, 1000 ,[count]);
31+
32+
return (
33+
{count}
34+
);
35+
}
36+
```

package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "use-debounced-effect",
3+
"version": "0.0.1",
4+
"description": "Debounced effect hook for react",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"build": "babel ./src --out-dir ./lib"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "[email protected]:samanmohamadi/use-debounced-effect.git"
12+
},
13+
"keywords": [
14+
"debounce",
15+
"use-effect",
16+
"react-hook",
17+
"react"
18+
],
19+
"author": "Saman Mohamadi ([email protected])",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/samanmohamadi/use-debounced-effect/issues"
23+
},
24+
"peerDependencies": {},
25+
"homepage": "https://github.com/samanmohamadi/use-debounced-effect#readme",
26+
"devDependencies": {
27+
"babel-cli": "^6.26.0",
28+
"babel-plugin-transform-object-rest-spread": "^6.26.0",
29+
"babel-preset-env": "^1.7.0"
30+
}
31+
}

src/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React, { useEffect } from 'react';
2+
3+
export function useDebouncedEffect(callback, delay,deps=[]) {
4+
5+
useEffect(
6+
() => {
7+
const handler = setTimeout(() => {
8+
callback()
9+
}, delay);
10+
11+
return () => {
12+
clearTimeout(handler);
13+
};
14+
},
15+
[callback,delay,...deps]
16+
);
17+
}
18+
19+
export default useDebouncedEffect;

0 commit comments

Comments
 (0)