This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
generated from satya164/typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a useIsFocused hook to get focus state (#52)
- Loading branch information
Showing
9 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as React from 'react'; | ||
import { render, act } from 'react-native-testing-library'; | ||
import useNavigationBuilder from '../useNavigationBuilder'; | ||
import useIsFocused from '../useIsFocused'; | ||
import NavigationContainer from '../NavigationContainer'; | ||
import Screen from '../Screen'; | ||
import MockRouter from './__fixtures__/MockRouter'; | ||
|
||
it('renders correct focus state', () => { | ||
const TestNavigator = (props: any): any => { | ||
const { state, descriptors } = useNavigationBuilder(MockRouter, props); | ||
|
||
return state.routes.map(route => descriptors[route.key].render()); | ||
}; | ||
|
||
const Test = () => { | ||
const isFocused = useIsFocused(); | ||
|
||
return ( | ||
<React.Fragment>{isFocused ? 'focused' : 'unfocused'}</React.Fragment> | ||
); | ||
}; | ||
|
||
const navigation = React.createRef<any>(); | ||
|
||
const root = render( | ||
<NavigationContainer ref={navigation}> | ||
<TestNavigator> | ||
<Screen name="first">{() => null}</Screen> | ||
<Screen name="second" component={Test} /> | ||
<Screen name="third">{() => null}</Screen> | ||
</TestNavigator> | ||
</NavigationContainer> | ||
); | ||
|
||
expect(root).toMatchInlineSnapshot(`"unfocused"`); | ||
|
||
act(() => navigation.current.navigate('second')); | ||
|
||
expect(root).toMatchInlineSnapshot(`"focused"`); | ||
|
||
act(() => navigation.current.navigate('third')); | ||
|
||
expect(root).toMatchInlineSnapshot(`"unfocused"`); | ||
|
||
act(() => navigation.current.navigate('second')); | ||
|
||
expect(root).toMatchInlineSnapshot(`"focused"`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as React from 'react'; | ||
import { useSubscription } from 'use-subscription'; | ||
import useNavigation from './useNavigation'; | ||
|
||
/** | ||
* Hook to get the current focus state of the screen. Returns a `true` if screen is focused, otherwise `false`. | ||
* This can be used if a component needs to render something based on the focus state. | ||
* It uses `use-subscription` under the hood for safer use in concurrent mode. | ||
*/ | ||
export default function useIsFocused(): boolean { | ||
const navigation = useNavigation(); | ||
const getCurrentValue = React.useCallback(navigation.isFocused, [navigation]); | ||
const subscribe = React.useCallback( | ||
(callback: (value: boolean) => void) => { | ||
const unsubscribeFocus = navigation.addListener('focus', () => | ||
callback(true) | ||
); | ||
|
||
const unsubscribeBlur = navigation.addListener('blur', () => | ||
callback(false) | ||
); | ||
|
||
return () => { | ||
unsubscribeFocus(); | ||
unsubscribeBlur(); | ||
}; | ||
}, | ||
[navigation] | ||
); | ||
|
||
return useSubscription({ | ||
getCurrentValue, | ||
subscribe, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
declare module 'use-subscription' { | ||
export function useSubscription<T>(options: { | ||
getCurrentValue: () => T; | ||
subscribe: (callback: (value: T) => void) => () => void; | ||
}): T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters