Skip to content
This repository was archived by the owner on Dec 3, 2022. It is now read-only.

Commit ab60ff2

Browse files
committed
fix code after review
1 parent 21d754e commit ab60ff2

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

src/Hooks.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
NavigationEventPayload,
1717
EventType,
1818
} from 'react-navigation';
19-
import { BackHandler } from 'react-native';
2019

2120
export function useNavigation<S>(): NavigationScreenProp<S & NavigationRoute> {
2221
const navigation = useContext(NavigationContext) as any; // TODO typing?
@@ -152,14 +151,13 @@ type EffectCallback = (() => void) | (() => () => void);
152151
// See https://github.com/react-navigation/hooks/issues/39#issuecomment-534694135
153152
export const useFocusEffect = (callback: EffectCallback) => {
154153
const navigation = useNavigation();
155-
const getCallback = useGetter(callback);
156154

157155
useEffect(() => {
158156
let isFocused = false;
159157
let cleanup: (() => void) | void;
160158

161159
if (navigation.isFocused()) {
162-
cleanup = getCallback()();
160+
cleanup = callback();
163161
isFocused = true;
164162
}
165163

@@ -171,7 +169,7 @@ export const useFocusEffect = (callback: EffectCallback) => {
171169
}
172170

173171
cleanup && cleanup();
174-
cleanup = getCallback()();
172+
cleanup = callback();
175173
isFocused = true;
176174
});
177175

@@ -186,34 +184,27 @@ export const useFocusEffect = (callback: EffectCallback) => {
186184
focusSubscription.remove();
187185
blurSubscription.remove();
188186
};
189-
}, [getCallback, navigation]);
187+
}, [callback, navigation]);
190188
};
191189

192190
export const useIsFocused = () => {
193191
const navigation = useNavigation();
192+
const getNavigation = useGetter(useNavigation());
194193
const [focused, setFocused] = useState(navigation.isFocused());
195194

196195
useEffect(() => {
197-
const focusSubscription = navigation.addListener('willFocus', () =>
198-
setFocused(true),
196+
const nav = getNavigation();
197+
const focusSubscription = nav.addListener('willFocus', () =>
198+
setFocused(true)
199199
);
200-
const blurSubscription = navigation.addListener('willBlur', () =>
201-
setFocused(false),
200+
const blurSubscription = nav.addListener('willBlur', () =>
201+
setFocused(false)
202202
);
203203
return () => {
204204
focusSubscription.remove();
205205
blurSubscription.remove();
206206
};
207-
}, [setFocused]);
207+
}, [getNavigation]);
208208

209209
return focused;
210210
};
211-
212-
export const useBackHandler = (backHandler: () => boolean) => {
213-
useFocusEffect(() => {
214-
BackHandler.addEventListener('hardwareBackPress', backHandler);
215-
return () => {
216-
BackHandler.removeEventListener('hardwareBackPress', backHandler);
217-
};
218-
});
219-
};

0 commit comments

Comments
 (0)