Search code examples
typescriptreact-nativereduxreact-redux

React Native - Handle session timeout after any api call


I have a complex react-native application and I'm using .net api for backend. When session timeout occurs on api side, I want to automatically logout the user and return to login page. But I can't use dispatch in the class that I'm doing api call. Because its not a component or a redux file. To navigate to the login page I need to dispatch some actions. Is there a way to do is? If I use Redux-Toolkit, does it work? I am looking for an easier solution because start using Redux toolkit in the project is a big effort for me right now.

Here is my project's structure:

enter image description here

Thanks in advance.


Solution

  • "But I can't use dispatch in the class that I'm doing api call." why not, you can dispatch everywhere,

    this is my code, you can try it

    //this is store
    export const store = createStore(
        persistedReducer,
        applyMiddleware(epicMiddleware)
    );
    
    //import to use anywhere
    import {store} from '../redux/store'
    store.dispatch(setUser(null)); // call when session out