Skip to content

Instantly share code, notes, and snippets.

@milankinen
Created August 14, 2019 10:15
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save milankinen/635b35f6f69d0c1b673ad81dfcf851a8 to your computer and use it in GitHub Desktop.
Modified useSubscription
@@ -23,8 +23,8 @@ export function useSubscription({ getCurrentValue, subscribe }) {
useEffect(() => {
let didUnsubscribe = false;
-
- const checkForUpdates = () => {
+ // NOTICE: value coming from subscription not from getCurrentValue !!!
+ const onValue = value => {
if (didUnsubscribe) {
return;
}
@@ -35,7 +35,7 @@ export function useSubscription({ getCurrentValue, subscribe }) {
) {
return prevState;
}
- const value = getCurrentValue();
+
if (prevState.value === value) {
return prevState;
}
@@ -43,8 +43,8 @@ export function useSubscription({ getCurrentValue, subscribe }) {
return { ...prevState, value };
});
};
- const unsubscribe = subscribe(checkForUpdates);
- checkForUpdates();
+ const unsubscribe = subscribe(onValue);
+ onValue(state.getCurrentValue());
return () => {
didUnsubscribe = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment