Skip to content
Closed
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
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,17 @@
"react-native-webview": "*"
},
"peerDependenciesMeta": {
"@react-navigation/native": {
"optional": true
},
"expo": {
"optional": true
},
"react-native-safe-area-context": {
"optional": true
},
"react-native-webview": {
"optional": true
}
},
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion src/inbox/components/IterableInbox.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useIsFocused } from '@react-navigation/native';
import { useEffect, useState } from 'react';
import {
Animated,
Expand All @@ -19,6 +18,7 @@ import { Iterable } from '../../core/classes/Iterable';
import { IterableInAppDeleteSource, IterableInAppLocation } from '../../inApp';

import { IterableInboxDataModel } from '../classes';
import { useIsFocused } from '../hooks';
import { ITERABLE_INBOX_COLORS } from '../constants';
import type {
IterableInboxCustomizations,
Expand Down
1 change: 1 addition & 0 deletions src/inbox/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useIsFocused } from './useIsFocused';
41 changes: 41 additions & 0 deletions src/inbox/hooks/useIsFocused.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Safe wrapper around `@react-navigation/native`'s `useIsFocused` hook.
*
* If `@react-navigation/native` is not installed (i.e., the consumer does not
* use React Navigation), the hook falls back to always returning `true` —
* meaning the component behaves as if it is always focused.
*
* This makes `@react-navigation/native` an optional peer dependency so that
* consumers who don't use the Inbox UI (or who use a different navigation
* library) are not forced to install it.
*
* @see https://github.com/Iterable/react-native-sdk/issues/770
*/

// Use globalThis.require to avoid needing @types/node in the tsconfig while
// still performing a synchronous optional require at module-load time.
declare const globalThis: { require?: (id: string) => unknown };

let useIsFocusedFromNav: (() => boolean) | undefined;

try {
const reactNavigation = globalThis.require?.('@react-navigation/native') as
| { useIsFocused?: () => boolean }
| undefined;
useIsFocusedFromNav = reactNavigation?.useIsFocused;
} catch {
// @react-navigation/native is not installed — that's fine.
}

/**
* Returns whether the screen is currently focused.
*
* Uses React Navigation's `useIsFocused` when available, otherwise defaults
* to `true`.
*/
export function useIsFocused(): boolean {
if (useIsFocusedFromNav) {
return useIsFocusedFromNav();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React Hook "useIsFocusedFromNav" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return? [eslint:react-hooks/rules-of-hooks]

}
return true;
}
Loading