Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HEAP-9449] Perform ref forwarding for react nav HOC #101

Merged
merged 2 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion examples/TestDriver/App.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import React from 'react';
import { Provider } from 'react-redux';
import { NavigationActions } from 'react-navigation';
import { store } from './src/reduxElements';
import TopNavigator from './src/topNavigator';

let _navigator;

function setTopLevelNavigator(navigatorRef) {
_navigator = navigatorRef;

navigate('Initial');
}

function navigate(routeName, params) {
console.log(_navigator);
_navigator.dispatch(
NavigationActions.navigate({
routeName,
params,
})
);
}

export default class App extends React.Component {
render() {
return (
<Provider store={store}>
<TopNavigator />
<TopNavigator ref={setTopLevelNavigator} />
</Provider>
);
}
Expand Down
23 changes: 19 additions & 4 deletions js/autotrack/reactNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ const EVENT_TYPE = 'reactNavigationScreenview';
const INITIAL_ROUTE_TYPE = 'Heap_Navigation/INITIAL';

export const withReactNavigationAutotrack = track => AppContainer => {
return class HeapNavigationWrapper extends React.Component {
class HeapNavigationWrapper extends React.Component {
topLevelNavigator = null;

setRef(ref, value) {
if (typeof ref === 'function') {
ref(value);
} else if (ref !== null) {
ref.current = value;
}
}

trackInitialRoute() {
const initialPageviewPath = getActiveRouteName(
this.topLevelNavigator.state.nav
Expand All @@ -28,14 +36,17 @@ export const withReactNavigationAutotrack = track => AppContainer => {
return this._render();
} catch (e) {
bail(e);
return <AppContainer />;
const { forwardedRef, ...rest } = this.props;
return <AppContainer ref={forwardedRef} {...rest} />;
}
}

_render() {
const { forwardedRef, ...rest } = this.props;
return (
<AppContainer
ref={bailOnError(navigatorRef => {
this.setRef(forwardedRef, navigatorRef);
if (this.topLevelNavigator !== navigatorRef) {
console.log(
'Heap: React Navigation is instrumented for autocapture.'
Expand All @@ -55,13 +66,17 @@ export const withReactNavigationAutotrack = track => AppContainer => {
});
}
})}
{...this.props}
{...rest}
>
{this.props.children}
</AppContainer>
);
}
};
}

return React.forwardRef((props, ref) => {
return <HeapNavigationWrapper {...props} forwardedRef={ref} />;
});
};

const getActiveRouteName = navigationState => {
Expand Down