-
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.
- replaced native
useId
from react-v18 to a non-native solution for…
… backward-compatibility support - general refactor, mainly DRY - added logic for unmounted components, so the watchers would be removed
- Loading branch information
Yair Even Or
authored and
Yair Even Or
committed
Oct 17, 2022
1 parent
cd4fabe
commit d659ed2
Showing
4 changed files
with
61 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
import {useId} from 'react' | ||
import {useEffect} from 'react' | ||
import {useId} from './utils' | ||
import {setWatcher, removeWatcher} from './propWatcher' | ||
|
||
/** | ||
* Similar to "useSmartRefListener" but just listens without automatically re-rendering (no 'useState') | ||
* Similar to "useWatchableListener" but just listens without automatically re-rendering (no 'useState') | ||
* @param {*} callback fires when a ref change detetced | ||
* @param {*} dependencies array of watchable "smart" refs | ||
* @param {*} dependencies array of watchable refs | ||
*/ | ||
const useWatchableEffect = (callback, dependencies) => { | ||
const id = useId() | ||
|
||
dependencies.forEach(ref => { | ||
// catch errors | ||
if( !ref ) { | ||
console.warn("useWatchableEffect - ref does not exists") | ||
return | ||
} | ||
useEffect(() => { | ||
// bind the callback to all dependencies | ||
dependencies.forEach(ref => { | ||
setWatcher(ref, id.current, callback) | ||
}) | ||
|
||
if( !ref.__WATCHERS ) { | ||
console.warn("useWatchableEffect - ref is not watchable. Did you pass the correct Object?") | ||
return | ||
// remove callback if component unmounted | ||
return () => { | ||
dependencies.forEach(ref => removeWatcher(ref, id.current)) | ||
} | ||
|
||
// register a listener for that namespace | ||
ref.__WATCHERS[id] = callback | ||
}) | ||
}, dependencies) | ||
} | ||
|
||
export default useWatchableEffect; | ||
export default useWatchableEffect |
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,7 @@ | ||
import {useRef} from 'react' | ||
|
||
// https://stackoverflow.com/a/7061193/104380 | ||
export const UUIDv4 = function b(a){return a?(a^Math.random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,b)}; | ||
|
||
// Native is preferable but only for React v18+, so for backward-compatibility, use this: | ||
export const useId = () => useRef(UUIDv4()).current; |