Skip to content

Commit 699511a

Browse files
committed
ChatScreen [nfc]: Replace useEdgeTriggeredEffect with useConditionalEffect
And remove useConditionalEffect; see the previous commit for why useConditionalEffect has a better interface.
1 parent 69a616b commit 699511a

File tree

2 files changed

+5
-37
lines changed

2 files changed

+5
-37
lines changed

src/chat/ChatScreen.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { getAuth, getCaughtUpForNarrow } from '../selectors';
2929
import { showErrorAlert } from '../utils/info';
3030
import { TranslationContext } from '../boot/TranslationProvider';
3131
import * as api from '../api';
32-
import { useEdgeTriggeredEffect } from '../reactUtils';
32+
import { useConditionalEffect } from '../reactUtils';
3333

3434
type Props = $ReadOnly<{|
3535
navigation: AppNavigationProp<'chat'>,
@@ -71,9 +71,9 @@ const useMessagesWithFetch = args => {
7171
// like using instance variables in class components:
7272
// https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables
7373
const shouldFetchWhenNextFocused = React.useRef<boolean>(false);
74-
const scheduleFetch = () => {
74+
const scheduleFetch = useCallback(() => {
7575
shouldFetchWhenNextFocused.current = true;
76-
};
76+
}, []);
7777

7878
const [fetchError, setFetchError] = React.useState<mixed>(null);
7979

@@ -92,7 +92,7 @@ const useMessagesWithFetch = args => {
9292
if (eventQueueId !== null) {
9393
scheduleFetch();
9494
}
95-
}, [eventQueueId]);
95+
}, [eventQueueId, scheduleFetch]);
9696

9797
// If we stop having any data at all about the messages in this narrow --
9898
// we don't know any, and nor do we know if there are some -- then
@@ -104,7 +104,7 @@ const useMessagesWithFetch = args => {
104104
// isFetching false, even though the fetch effect will cause a rerender
105105
// with isFetching true. It'd be nice to avoid that.
106106
const nothingKnown = messages.length === 0 && !caughtUp.older && !caughtUp.newer;
107-
useEdgeTriggeredEffect(scheduleFetch, nothingKnown, true);
107+
useConditionalEffect(scheduleFetch, nothingKnown);
108108

109109
// On first mount, fetch. (This also makes a fetch no longer scheduled,
110110
// so the if-scheduled fetch below doesn't also fire.)

src/reactUtils.js

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -84,38 +84,6 @@ export const useHasStayedTrueForMs = (value: boolean, duration: number): boolean
8484
return result;
8585
};
8686

87-
/**
88-
* Invoke the callback as an effect when `value` turns from false to true.
89-
*
90-
* This differs from putting `value` in a `useEffect` dependency list in
91-
* that the callback will *not* be invoked when the value changes in the
92-
* other direction, from true to false.
93-
*
94-
* `includeStart` controls what happens if `value` is true on the very first
95-
* render. If `includeStart` is true, then the effect is triggered (so
96-
* treating its previous state of nonexistence as equivalent to being
97-
* false). If `includeStart` is false, then the effect is not triggered (so
98-
* treating its previous state of nonexistence as not a valid state to
99-
* compare to.)
100-
*
101-
* The callback is not permitted to return a cleanup function, because it's
102-
* not clear what the semantics should be of when such a cleanup function
103-
* would be run.
104-
*/
105-
export const useEdgeTriggeredEffect = (
106-
cb: () => void,
107-
value: boolean,
108-
includeStart: boolean,
109-
): void => {
110-
const prev = usePrevious(value, !includeStart);
111-
useEffect(() => {
112-
if (value && !prev) {
113-
cb();
114-
}
115-
// No dependencies list -- the effect itself decides whether to act.
116-
});
117-
};
118-
11987
/**
12088
* Like `useEffect`, but the callback only runs when `value` is true.
12189
*

0 commit comments

Comments
 (0)