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-42725] Return the same component each time from withHeapNavigationWrapper #406

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- `Heap.withHeapNavigationWrapper` returns the same object when called
multiple times, preventing app refreshes when used within a
re-evaluated function like `App` with `useEffect`.

## [0.22.4] - 2023-09-05

### Fixed
Expand Down
24 changes: 24 additions & 0 deletions js/__tests__/Heap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,28 @@ describe('The Heap object', () => {
expect(() => Heap.clearEventProperties()).not.toThrow();
});
});

describe('withReactNavigationAutotrack', () => {

it('returns a different HOC for each passed in AppContainer', () => {

function Component1() {}
function Component2() {}

const Wrapped1 = Heap.withReactNavigationAutotrack(Component1);
const Wrapped2 = Heap.withReactNavigationAutotrack(Component2);

expect(Wrapped1).not.toStrictEqual(Wrapped2);
});

it('returns the same HOC when given the same AppContainer', () => {

function Component1() {}

const Wrapped1 = Heap.withReactNavigationAutotrack(Component1);
const Wrapped2 = Heap.withReactNavigationAutotrack(Component1);

expect(Wrapped1).toStrictEqual(Wrapped2);
});
});
});
8 changes: 7 additions & 1 deletion js/autotrack/reactNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const EVENT_TYPE = 'react_navigation_screenview';
const INITIAL_ROUTE_TYPE = 'Heap_Navigation/INITIAL';

export const withReactNavigationAutotrack = track => AppContainer => {

const existingWrapper = AppContainer.__heapWrapper;
if (existingWrapper) {
return existingWrapper;
}

const captureOldNavigationStateChange = bailOnError((prev, next, action) => {
const { screen_path: prevScreenRoute } = NavigationUtil.getActiveRouteProps(
prev
Expand Down Expand Up @@ -153,7 +159,7 @@ export const withReactNavigationAutotrack = track => AppContainer => {
AppContainer
)})`;

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