update subscriptiogn removal for feed screen components

This commit is contained in:
Nouman Tahir 2022-10-24 18:17:10 +05:00
parent 7c5d9cb83b
commit 6e7f0f04cd
2 changed files with 12 additions and 8 deletions

View File

@ -49,12 +49,14 @@ const PostsContainer = ({
feedSubfilterOptionsValue,
isFeedScreen = false,
}) => {
const appState = useRef(AppState.currentState);
const dispatch = useDispatch();
const intl = useIntl();
let _postFetchTimer = null;
const appState = useRef(AppState.currentState);
const appStateSubRef = useRef(null);
const nsfw = useSelector((state) => state.application.nsfw);
const initPosts = useSelector((state) => state.posts.initPosts);
const isConnected = useSelector((state) => state.application.isConnected);
@ -295,8 +297,9 @@ const PostsContainer = ({
const isMountedRef = useIsMountedRef();
useEffect(() => {
let appStateSub;
if (isFeedScreen) {
AppState.addEventListener('change', _handleAppStateChange);
appStateSub = AppState.addEventListener('change', _handleAppStateChange);
_setFeedPosts(initPosts || []);
} else {
_setFeedPosts([]);
@ -306,8 +309,8 @@ const PostsContainer = ({
if (_postFetchTimer) {
clearTimeout(_postFetchTimer);
}
if (isFeedScreen) {
AppState.removeEventListener('change', _handleAppStateChange);
if (isFeedScreen && appStateSub) {
appStateSub.remove();
}
};
}, []);

View File

@ -1,6 +1,6 @@
import React, { useState, useEffect, useRef } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { AppState, NativeScrollEvent, NativeSyntheticEvent } from 'react-native';
import { AppState, NativeEventSubscription, NativeScrollEvent, NativeSyntheticEvent } from 'react-native';
import { debounce } from 'lodash';
import PostsList from '../../postsList';
import { fetchPromotedEntries, loadPosts } from '../services/tabbedPostsFetch';
@ -63,6 +63,7 @@ const TabContent = ({
//refs
let postsListRef = useRef<PostsListRef>();
const appState = useRef(AppState.currentState);
const appStateSubRef = useRef<NativeEventSubscription|null>()
const postsRef = useRef(posts);
const sessionUserRef = useRef(sessionUser);
@ -73,7 +74,7 @@ const TabContent = ({
//side effects
useEffect(() => {
if (isFeedScreen) {
AppState.addEventListener('change', _handleAppStateChange);
appStateSubRef.current = AppState.addEventListener('change', _handleAppStateChange);
}
_initContent(true, feedUsername);
@ -110,8 +111,8 @@ const TabContent = ({
if (postFetchTimer) {
clearTimeout(postFetchTimer);
}
if (isFeedScreen) {
AppState.removeEventListener('change', _handleAppStateChange);
if (isFeedScreen && appStateSubRef.current) {
appStateSubRef.current.remove();
}
};