From 6e7f0f04cdb8fa3f10829c5c008736533e2fca5b Mon Sep 17 00:00:00 2001 From: Nouman Tahir Date: Mon, 24 Oct 2022 18:17:10 +0500 Subject: [PATCH] update subscriptiogn removal for feed screen components --- src/components/posts/container/postsContainer.js | 11 +++++++---- src/components/tabbedPosts/view/tabContent.tsx | 9 +++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/components/posts/container/postsContainer.js b/src/components/posts/container/postsContainer.js index 9f9be401d..51b782d07 100644 --- a/src/components/posts/container/postsContainer.js +++ b/src/components/posts/container/postsContainer.js @@ -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(); } }; }, []); diff --git a/src/components/tabbedPosts/view/tabContent.tsx b/src/components/tabbedPosts/view/tabContent.tsx index 69a41333e..322c3ff2c 100644 --- a/src/components/tabbedPosts/view/tabContent.tsx +++ b/src/components/tabbedPosts/view/tabContent.tsx @@ -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(); const appState = useRef(AppState.currentState); + const appStateSubRef = useRef() 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(); } };