DEV Community

Akash Yadav
Akash Yadav

Posted on

useRefreshScreensStore Hook

import { useState, useEffect } from 'react';
import { getFocusedRouteNameFromRoute, useRoute } from '@react-navigation/native';
import { useRefreshScreensStore } from '../stores/stores';

function useRefreshScreens() {
    const [shouldRefresh, setShouldRefresh] = useState(false);

    const refreshScreen = useRefreshScreensStore(state => state.refreshScreen);
    const scheduleRefresh = useRefreshScreensStore(state => state.scheduleRefresh);
    const refreshScreens = useRefreshScreensStore(state => state.refreshScreens);

    const route = useRoute();

    const scheduleRefreshScreen = (screen) => {
        scheduleRefresh(screen);
    }

    useEffect(() => {
        const routeName = getFocusedRouteNameFromRoute(route) || route.name;

        if (refreshScreens.includes(routeName)) { setShouldRefresh(true); refreshScreen(routeName) }
        else { setShouldRefresh(false) }
    }, [route, refreshScreens, refreshScreen]);

    return { shouldRefresh, scheduleRefreshScreen };
}

export default useRefreshScreens;
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay