Skip to content

Instantly share code, notes, and snippets.

@one-more
Last active April 10, 2025 05:08
Show Gist options
  • Save one-more/95106d5cedc1f184fe58303cdb3333d8 to your computer and use it in GitHub Desktop.
Save one-more/95106d5cedc1f184fe58303cdb3333d8 to your computer and use it in GitHub Desktop.
subscribe to (redux)store with selector
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