Add state updates after screen changes

This commit is contained in:
Paul Frazee 2022-07-25 14:21:48 -05:00
parent 7f04ac172e
commit 3794eca88e
7 changed files with 171 additions and 52 deletions

View file

@ -1,6 +1,7 @@
import React, {useState, useEffect} from 'react'
import {observer} from 'mobx-react-lite'
import {ActivityIndicator, FlatList, Text, View} from 'react-native'
import {useFocusEffect} from '@react-navigation/native'
import {OnNavigateContent} from '../../routes/types'
import {
PostThreadViewModel,
@ -9,6 +10,8 @@ import {
import {useStores} from '../../../state'
import {PostThreadItem} from './PostThreadItem'
const UPDATE_DELAY = 2e3 // wait 2s before refetching the thread for updates
export const PostThread = observer(function PostThread({
uri,
onNavigateContent,
@ -18,6 +21,7 @@ export const PostThread = observer(function PostThread({
}) {
const store = useStores()
const [view, setView] = useState<PostThreadViewModel | undefined>()
const [lastUpdate, setLastUpdate] = useState<number>(Date.now())
useEffect(() => {
if (view?.params.uri === uri) {
@ -30,6 +34,13 @@ export const PostThread = observer(function PostThread({
newView.setup().catch(err => console.error('Failed to fetch thread', err))
}, [uri, view?.params.uri, store])
useFocusEffect(() => {
if (Date.now() - lastUpdate > UPDATE_DELAY) {
view?.update()
setLastUpdate(Date.now())
}
})
// loading
// =
if (