React native 如何创建具有useEffect和react导航焦点侦听器的函数

React native 如何创建具有useEffect和react导航焦点侦听器的函数,react-native,react-hooks,react-navigation,React Native,React Hooks,React Navigation,我目前正在构建一个应用程序,它在每个屏幕上都会在聚焦时获取数据 useEffect(() => { const unsubscribe = navigation.addListener('focus', () => { //fetching some data }); // Return the function to unsubscribe from the event so it gets removed on unmount r

我目前正在构建一个应用程序,它在每个屏幕上都会在聚焦时获取数据

useEffect(() => {
    const unsubscribe = navigation.addListener('focus', () => {
        //fetching some data
    });

    // Return the function to unsubscribe from the event so it gets removed on unmount
    return unsubscribe;
}, [navigation]);
如何将useffect和这个unsubscribe变量结合起来,这样我就不必在每个屏幕上复制粘贴上面的代码块了。基本上我是在努力避免重复我自己

更新

我在react导航文档中找到了以下功能

它基本上就像useEffect一样,从代码阅读的角度来看,它更容易阅读(在我看来)

useFocusEffect(
    React.useCallback(() => {
        console.log('is focused')
        const unsubscribe = () => console.log('unsubbed focus effect')
        return () => unsubscribe();
    }, [])
);