Skip to content

Commit 7d3cd45

Browse files
committed
feat: Support expo-router focus events
1 parent 6e9d36b commit 7d3cd45

File tree

3 files changed

+802
-25
lines changed

3 files changed

+802
-25
lines changed

packages/react/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
"peerDependencies": {
134134
"@react-navigation/native": "^6.0.0",
135135
"@types/react": "^16.8.4 || ^17.0.0 || ^18.0.0 || ^19.0.0",
136+
"expo-router": "^3.0.0",
136137
"react": "^16.8.4 || ^17.0.0 || ^18.0.0 || ^19.0.0"
137138
},
138139
"peerDependenciesMeta": {
@@ -141,13 +142,17 @@
141142
},
142143
"@types/react": {
143144
"optional": true
145+
},
146+
"expo-router": {
147+
"optional": true
144148
}
145149
},
146150
"devDependencies": {
147151
"@anansi/browserslist-config": "^1.4.2",
148152
"@react-navigation/native": "^6.1.6",
149153
"@types/node": "^20.0.0",
150154
"@types/react": "^18.0.30",
155+
"expo-router": "^3.4.8",
151156
"react-native": "^0.74.0"
152157
}
153158
}

packages/react/src/hooks/useFocusEffect.native.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
1-
import type { useNavigation, NavigationProp } from '@react-navigation/native';
21
import {
32
type DependencyList,
43
type EffectCallback,
54
useEffect,
65
useRef,
76
} from 'react';
87

9-
const fakeNavigation = { addListener(name: string) {} } as any;
10-
let _useNavigation: typeof useNavigation = () => fakeNavigation;
11-
import('@react-navigation/native')
12-
.then(rn => {
13-
_useNavigation = rn.useNavigation;
8+
const fakeNavigation = {
9+
addListener(name: string, cb: () => void) {
10+
return () => {};
11+
},
12+
};
13+
let _useNavigation = () => fakeNavigation;
14+
15+
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
16+
// if they are using expo-router they are more likely wanting to use that implementation
17+
// as react-navigation can often appear in node_modules when not used
18+
import('expo-router')
19+
.then(router => {
20+
_useNavigation = router.useNavigation;
21+
console.log('hi', _useNavigation, router);
1422
})
15-
.catch(() => {});
23+
.catch(e => {
24+
console.log('caught error', e);
25+
import('@react-navigation/native')
26+
.then(rn => {
27+
_useNavigation = rn.useNavigation;
28+
})
29+
.catch(() => {});
30+
});
1631

1732
function useFocusEffect(
1833
effect: EffectCallback,
1934
deps?: DependencyList,
2035
runOnMount = false,
2136
) {
22-
let navigation: NavigationProp<ReactNavigation.RootParamList>;
37+
let navigation: typeof fakeNavigation;
2338
// if we aren't in react-navigation context, just ignore focus events
2439
try {
2540
navigation = _useNavigation();

0 commit comments

Comments
 (0)