Skip to content

Commit ec61e6d

Browse files
committed
feat(hooks): Apply PR feedback
- rename ComponentIdContext by NavigationContext - remove useCompoentId hook just use useContext(NavigationContext) - rename withComponentIdProvider hoc
1 parent cb1172a commit ec61e6d

12 files changed

+57
-76
lines changed

src/contexts/ComponentIdContext.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/contexts/NavigationContext.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { createContext } from 'react'
2+
3+
export type NavigationContext = {
4+
componentId?: string
5+
}
6+
7+
const NavigationContext = createContext<NavigationContext>({})
8+
9+
export const NavigationProvider = NavigationContext.Provider
10+
export const NavigationConsumer = NavigationContext.Consumer
11+
12+
export default NavigationContext

src/contexts/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default as ComponentIdContext, ComponentIdProvider, ComponentIdConsumer } from './ComponentIdContext'
1+
export { default as NavigationContext, NavigationProvider, NavigationConsumer } from './NavigationContext'

src/hocs/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default as withComponentIdProvider } from './withComponentIdProvider'
1+
export { default as withNavigationProvider } from './withNavigationProvider'

src/hocs/withComponentIdProvider.tsx

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/hocs/withComponentIdProvider.test.tsx renamed to src/hocs/withNavigationProvider.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import React from 'react'
22
import { render, cleanup } from '@testing-library/react-native'
3-
import withComponentIdProvider from './withComponentIdProvider'
3+
import withNavigationProvider from './withNavigationProvider'
44
import { View } from 'react-native'
5-
import { ComponentIdConsumer } from '../contexts/ComponentIdContext'
5+
import { NavigationConsumer } from '../contexts/NavigationContext'
66

7-
describe('withComponentIdProvider', () => {
7+
describe('withNavigationProvider', () => {
88
afterEach(cleanup)
99

10-
it('should wrap compenent with ComponentIdProvider and bypass props', () => {
10+
it('should wrap compenent with NavigationProvider and bypass props', () => {
1111
const Component = ({ componentId, prop1 }) => (
1212
<View testID={`root-${componentId}`}>
13-
<ComponentIdConsumer>{id => <View testID={`with-${id}-${prop1}`} />}</ComponentIdConsumer>
13+
<NavigationConsumer>{({ componentId: id }) => <View testID={`with-${id}-${prop1}`} />}</NavigationConsumer>
1414
</View>
1515
)
1616

17-
const WrappedComponent = withComponentIdProvider(Component)
17+
const WrappedComponent = withNavigationProvider(Component)
1818

1919
const { getByTestId } = render(<WrappedComponent componentId="id" prop1="value1" />)
2020

src/hocs/withNavigationProvider.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react'
2+
import { NavigationFunctionComponent } from 'react-native-navigation'
3+
import hoistNonReactStatic from 'hoist-non-react-statics'
4+
import { NavigationProvider } from '../contexts/NavigationContext'
5+
import setCompositionDisplayName from '../helpers/setCompositionDisplayName'
6+
7+
/**
8+
* A public higher-order component to access the NavigationContext componentId value
9+
*/
10+
const withNavigationProvider = <P extends {}>(WrappedComponent: NavigationFunctionComponent<P>) => {
11+
const NavigationProviderContainer: NavigationFunctionComponent<P> = ({ componentId, ...props }) => (
12+
<NavigationProvider value={{ componentId }}>
13+
<WrappedComponent componentId={componentId} {...(props as P)} />
14+
</NavigationProvider>
15+
)
16+
17+
setCompositionDisplayName(NavigationProviderContainer, WrappedComponent)
18+
19+
hoistNonReactStatic(NavigationProviderContainer, WrappedComponent)
20+
21+
return NavigationProviderContainer
22+
}
23+
24+
export default withNavigationProvider

src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { default as useNavigation } from './useNavigation'
12
export { default as useNavigationComponentDidAppear } from './useNavigationComponentDidAppear'
23
export { default as useNavigationComponentDidDisappear } from './useNavigationComponentDidDisappear'
34
export { default as useNavigationCommand } from './useNavigationCommand'

src/hooks/useComponentId.test.tsx

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/hooks/useComponentId.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)