React Redux - send a call without connecting (not from the responding component)

I have a scheduled function that retrieves data from the server every x minutes, and I want it to update the state with a response:

api.fetchData().then(
    (response) => {
        dispatch(Actions.updateConfiguration(response));
    }
};

This function should not be a responsive component because it does not display anything.

However, I cannot figure out how to introduce a dispatchfunction without use connect, which requires it to be a reactive component.

Thank.

+4
source share
1 answer

We have a similar thing (repeating verification that the authentication token is still valid), and we did the following.

"util" redux

export function startSessionTimer(store) {
    const intervalId = setInterval(() => {
        if ( ... ) {
            store.dispatch( ... );
            clearInterval(intervalId);
        }
    }, (15 * 60 * 1000));
}

, , .

const store = configureStore( ... );
startSessionTimer(store);
+4

Source: https://habr.com/ru/post/1648374/


All Articles