3b7806963a
* Refresh on foreground * Add back interval
27 lines
722 B
TypeScript
27 lines
722 B
TypeScript
import {useEffect, useState} from 'react'
|
|
import {AppState, type AppStateStatus} from 'react-native'
|
|
|
|
export const getCurrentState = () => AppState.currentState
|
|
|
|
export function onAppStateChange(cb: (state: AppStateStatus) => void) {
|
|
let prev = AppState.currentState
|
|
return AppState.addEventListener('change', next => {
|
|
if (next === prev) return
|
|
prev = next
|
|
cb(next)
|
|
})
|
|
}
|
|
|
|
export function useOnAppStateChange(cb: (state: AppStateStatus) => void) {
|
|
useEffect(() => {
|
|
const sub = onAppStateChange(next => cb(next))
|
|
return () => sub.remove()
|
|
}, [cb])
|
|
}
|
|
|
|
export function useAppState() {
|
|
const [state, setState] = useState(AppState.currentState)
|
|
useOnAppStateChange(setState)
|
|
return state
|
|
}
|