Skip to content

Commit 9bdbf4c

Browse files
Yair Even OrYair Even Or
Yair Even Or
authored and
Yair Even Or
committed
-
1 parent e74f0ba commit 9bdbf4c

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

react-bouncer.js

Lines changed: 36 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react-bouncer.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/react-bouncer.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {useEffect, useCallback, useMemo, useReducer} from 'react'
2+
import debounce from 'lodash.debounce'
3+
4+
const bouncer = (Comp, duration = 300, method = debounce) => props => {
5+
// https://reactjs.org/docs/hooks-faq.html#is-there-something-like-forceupdate
6+
const [dep, forceUpdate] = useReducer(x => ++x, 0)
7+
const updater = useCallback(method(forceUpdate, duration), [])
8+
9+
// call the delayed function when props change (or was re-rendered without any props)
10+
useEffect(updater, [props])
11+
12+
return useMemo(() => <Comp {...props}/>, [dep])
13+
}
14+
15+
export default bouncer

0 commit comments

Comments
 (0)