1- import { ReactElement , ReactNode } from 'react' ;
1+ import { ReactElement , ReactNode , useEffect , useState } from 'react' ;
22import { AppStateProvider } from './useIsAppForeground' ;
33import { VisibilityChangedProvider } from './useVisibilityChanged' ;
4+ import { nativeEventEmitter } from '../native-event-emitter/nativeEventEmitter' ;
45
56interface Props {
67 isVisible : boolean ;
@@ -11,8 +12,9 @@ interface Props {
1112 * @name VisibilityProvider
1213 * @description
1314 * A Provider that manages whether a ReactNative view is currently in the foreground state.
15+ * It subscribes to the app's `visibilityChanged` event to detect and manage screen visibility.
1416 * @param {boolean } isVisible - Whether the app is in the foreground state.
15- * @param {ReactNode | undefined } children - Child components that observe `AppState`.
17+ * @param {ReactNode | undefined } children - Child components that observe `visibilityChanged` and ` AppState` event .
1618 * @returns {ReactElement } - A React Provider component wrapped with `VisibilityChangedProvider`.
1719 * @example
1820 * ```typescript
@@ -28,8 +30,20 @@ interface Props {
2830 * ```
2931 */
3032export function VisibilityProvider ( { isVisible, children } : Props ) : ReactElement {
33+ const [ visible , setVisible ] = useState ( isVisible ) ;
34+
35+ useEffect ( ( ) => {
36+ const subscription = nativeEventEmitter . addListener ( 'visibilityChanged' , ( nextVisible ) => {
37+ setVisible ( nextVisible ) ;
38+ } ) ;
39+
40+ return ( ) => {
41+ subscription . remove ( ) ;
42+ } ;
43+ } , [ ] ) ;
44+
3145 return (
32- < VisibilityChangedProvider isVisible = { isVisible } >
46+ < VisibilityChangedProvider isVisible = { visible } >
3347 < AppStateProvider > { children } </ AppStateProvider >
3448 </ VisibilityChangedProvider >
3549 ) ;
0 commit comments