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 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
import * as React from 'react'; | ||
import { NavigationProp, ParamListBase } from './types'; | ||
import { | ||
NavigationProp, | ||
ParamListBase, | ||
PartialState, | ||
Route, | ||
NavigationState, | ||
} from './types'; | ||
|
||
/** | ||
* Context which holds the navigation prop for a screen. | ||
*/ | ||
const NavigationContext = React.createContext< | ||
NavigationProp<ParamListBase, string, any, any> | undefined | ||
>(undefined); | ||
const NavigationContext = React.createContext<{ | ||
navigation: NavigationProp<ParamListBase, string, any, any> | undefined; | ||
route: | ||
| Route<string> & { | ||
state?: NavigationState | PartialState<NavigationState>; | ||
} | ||
| undefined; | ||
}>({ navigation: undefined, route: undefined }); | ||
|
||
export default NavigationContext; |
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,34 @@ | ||
import * as React from 'react'; | ||
import { render } from 'react-native-testing-library'; | ||
import useNavigationBuilder from '../useNavigationBuilder'; | ||
import useRoute from '../useRoute'; | ||
import NavigationContainer from '../NavigationContainer'; | ||
import Screen from '../Screen'; | ||
import MockRouter from './__fixtures__/MockRouter'; | ||
import { RouteProp } from '../types'; | ||
|
||
it('gets route prop from context', () => { | ||
expect.assertions(1); | ||
|
||
const TestNavigator = (props: any): any => { | ||
const { state, descriptors } = useNavigationBuilder(MockRouter, props); | ||
|
||
return state.routes.map(route => descriptors[route.key].render()); | ||
}; | ||
|
||
const Test = () => { | ||
const route = useRoute<RouteProp<{ sample: { x: string } }, 'sample'>>(); | ||
|
||
expect(route && route.params && route.params.x).toEqual(1); | ||
|
||
return null; | ||
}; | ||
|
||
render( | ||
<NavigationContainer> | ||
<TestNavigator> | ||
<Screen name="foo" component={Test} initialParams={{ x: 1 }} /> | ||
</TestNavigator> | ||
</NavigationContainer> | ||
); | ||
}); |
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
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,22 @@ | ||
import * as React from 'react'; | ||
import NavigationContext from './NavigationContext'; | ||
import { ParamListBase, RouteProp } from './types'; | ||
|
||
/** | ||
* Hook to access the route prop of the parent screen anywhere. | ||
* | ||
* @returns Route prop of the parent screen. | ||
*/ | ||
export default function useRoute< | ||
T extends RouteProp<ParamListBase, string> | ||
>(): T { | ||
const { route } = React.useContext(NavigationContext); | ||
|
||
if (route === undefined) { | ||
throw new Error( | ||
"We couldn't find a route object. Is your component inside a navigator?" | ||
); | ||
} | ||
|
||
return route as T; | ||
} |