Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLAT-11855] Forward navigation refs (#431)
fix: 🐛 Fix an issue where refs are not forwarded to the NavigationContainer
- Loading branch information
1 parent
75a38e9
commit 9388f1c
Showing
4 changed files
with
61 additions
and
36 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
58 changes: 29 additions & 29 deletions
58
packages/plugin-react-navigation/lib/create-navigation-container.tsx
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,44 +1,44 @@ | ||
import { type SpanFactory } from '@bugsnag/core-performance' | ||
import { type ReactNativeConfiguration } from '@bugsnag/react-native-performance' | ||
import { NavigationContainer, useNavigationContainerRef, type NavigationContainerProps } from '@react-navigation/native' | ||
import { NavigationContainer, useNavigationContainerRef, type NavigationContainerProps, type NavigationContainerRefWithCurrent } from '@react-navigation/native' | ||
import React, { forwardRef, useRef } from 'react' | ||
import { NavigationContextProvider } from './navigation-context' | ||
|
||
// Prevent rollup plugin from tree shaking NavigationContextProvider | ||
const Provider = NavigationContextProvider | ||
|
||
export const createNavigationContainer = (Container = NavigationContainer, spanFactory: SpanFactory<ReactNativeConfiguration>) => | ||
forwardRef<typeof Container, NavigationContainerProps>( | ||
(props, _ref) => { | ||
const { onStateChange, ...rest } = props | ||
type CreateNavigationContainer = (NavigationContainerComponent: typeof NavigationContainer, spanFactory: SpanFactory<ReactNativeConfiguration>) => typeof NavigationContainer | ||
type NavigationContainerRef = NavigationContainerRefWithCurrent<ReactNavigation.RootParamList> | ||
|
||
const navigationContainerRef = useNavigationContainerRef() | ||
const [routeName, setRouteName] = React.useState<string>() | ||
const routeNameRef = useRef<string>() | ||
export const createNavigationContainer: CreateNavigationContainer = (NavigationContainerComponent = NavigationContainer, spanFactory: SpanFactory<ReactNativeConfiguration>) => { | ||
return forwardRef<NavigationContainerRef, NavigationContainerProps>((props, _ref) => { | ||
const { onStateChange, ...rest } = props | ||
|
||
const wrappedOnStateChange: typeof onStateChange = (...args) => { | ||
const currentRoute = navigationContainerRef.current | ||
? navigationContainerRef.current.getCurrentRoute() | ||
: null | ||
const navigationContainerRef = _ref as NavigationContainerRef || useNavigationContainerRef() | ||
const [routeName, setRouteName] = React.useState<string>() | ||
const routeNameRef = useRef<string>() | ||
|
||
if (currentRoute) { | ||
routeNameRef.current = currentRoute.name | ||
setRouteName(currentRoute.name) | ||
} | ||
const wrappedOnStateChange: typeof onStateChange = (...args) => { | ||
const currentRoute = navigationContainerRef ? navigationContainerRef.getCurrentRoute() : null | ||
|
||
if (typeof onStateChange === 'function') { | ||
onStateChange.apply(this, args) | ||
} | ||
if (currentRoute) { | ||
routeNameRef.current = currentRoute.name | ||
setRouteName(currentRoute.name) | ||
} | ||
|
||
return ( | ||
<Provider spanFactory={spanFactory} currentRoute={routeName}> | ||
<Container | ||
{...rest} | ||
onStateChange={wrappedOnStateChange} | ||
ref={navigationContainerRef} | ||
/> | ||
</Provider> | ||
) | ||
if (typeof onStateChange === 'function') { | ||
onStateChange.apply(this, args) | ||
} | ||
} | ||
) | ||
|
||
return ( | ||
<Provider spanFactory={spanFactory} currentRoute={routeName}> | ||
<NavigationContainerComponent | ||
{...rest} | ||
onStateChange={wrappedOnStateChange} | ||
ref={navigationContainerRef} | ||
/> | ||
</Provider> | ||
) | ||
}) as typeof NavigationContainerComponent | ||
} |
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