@reduxjs/toolkit#Unsubscribe TypeScript Examples

The following examples show how to use @reduxjs/toolkit#Unsubscribe. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Tools.ts    From foundation-lib-spa-core with Apache License 2.0 6 votes vote down vote up
export function observeStore<T = any, S = any, A extends Action<any> = AnyAction, M extends Middlewares<S> = Middlewares<S>>
(   
    store: EnhancedStore<S, A, M>, 
    select: (state: S) => T, 
    onChange: (state: T) => void
) : Unsubscribe
{
    let currentState : any;

    function handleChange() {
        const nextState = select(store.getState());
        if (nextState !== currentState) {
            currentState = nextState;
            onChange(currentState);
        }
    }

    const unsubscribe = store.subscribe(handleChange);
    handleChange();
    return unsubscribe;
}
Example #2
Source File: Tools.d.ts    From foundation-lib-spa-core with Apache License 2.0 5 votes vote down vote up
export declare function observeStore<T = any, S = any, A extends Action<any> = AnyAction, M extends Middlewares<S> = Middlewares<S>>(store: EnhancedStore<S, A, M>, select: (state: S) => T, onChange: (state: T) => void): Unsubscribe;