Skip to content

Commit 6c621fd

Browse files
fixing issues with appstate listener
1 parent 8ca441b commit 6c621fd

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

src/components/Channel/Channel.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,8 @@ const ChannelWithContext = <
883883
const resyncChannel = async () => {
884884
if (!channel) return;
885885

886+
setError(false);
886887
try {
887-
setError(false);
888888
/**
889889
* Allow a buffer of 30 new messages, so that MessageList won't move its scroll position,
890890
* giving smooth user experience.
@@ -935,7 +935,7 @@ const ChannelWithContext = <
935935
newListBottomMessageCreatedAt >= oldListBottomMessageCreatedAt
936936
) {
937937
const index = state.messages.findIndex(
938-
(m) => m.id === oldListTopMessageId,
938+
(message) => message.id === oldListTopMessageId,
939939
);
940940
finalMessages = state.messages.slice(index);
941941
} else {

src/components/ChannelList/hooks/usePaginatedChannels.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ export const usePaginatedChannels = <
9494
newOptions,
9595
);
9696

97-
channelQueryResponse.forEach((c) => c.state.setIsUpToDate(true));
97+
channelQueryResponse.forEach((channel) =>
98+
channel.state.setIsUpToDate(true),
99+
);
98100

99101
const newChannels =
100102
queryType === 'reload' || queryType === 'refresh'
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
import { useEffect, useRef } from 'react';
1+
import { useEffect, useState } from 'react';
22
import { AppState, AppStateStatus } from 'react-native';
33

44
export const useAppStateListener = (
55
onForeground?: () => void,
66
onBackground?: () => void,
77
) => {
8-
const appState = useRef(AppState.currentState);
9-
const listenerExists = onForeground || onBackground;
10-
11-
useEffect(() => {
12-
listenerExists && AppState.addEventListener('change', handleAppStateChange);
13-
14-
return () =>
15-
listenerExists &&
16-
AppState.removeEventListener('change', handleAppStateChange);
17-
}, []);
8+
const [appState, setAppState] = useState(AppState.currentState);
189

1910
const handleAppStateChange = (nextAppState: AppStateStatus) => {
2011
if (
21-
appState.current === 'background' &&
12+
appState === 'background' &&
2213
nextAppState === 'active' &&
2314
onForeground
2415
) {
2516
onForeground();
2617
} else if (
27-
appState.current.match(/active|inactive/) &&
18+
appState.match(/active|inactive/) &&
2819
nextAppState === 'background' &&
2920
onBackground
3021
) {
3122
onBackground();
3223
}
33-
appState.current = nextAppState;
24+
setAppState(nextAppState);
3425
};
26+
27+
useEffect(() => {
28+
AppState.addEventListener('change', handleAppStateChange);
29+
30+
return () => AppState.removeEventListener('change', handleAppStateChange);
31+
}, [handleAppStateChange]);
3532
};

0 commit comments

Comments
 (0)