Last active
April 10, 2025 05:08
-
-
Save one-more/95106d5cedc1f184fe58303cdb3333d8 to your computer and use it in GitHub Desktop.
subscribe to (redux)store with selector
This file contains hidden or 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
export function subscribeWithSelector(selector: Function, cb: Function): Unsubscribe { | |
let prevValue = selector( | |
store.getState() | |
); | |
return store.subscribe(() => { | |
const newValue = selector( | |
store.getState() | |
); | |
if (newValue != prevValue) { | |
prevValue = newValue; | |
cb( | |
newValue, | |
) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment