@reduxjs/toolkit#EnhancedStore TypeScript Examples
The following examples show how to use
@reduxjs/toolkit#EnhancedStore.
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: index.ts From Teyvat.moe with GNU General Public License v3.0 | 6 votes |
addWatchers = (store: EnhancedStore): void => {
// Call these functions whenever the store changes.
for (const watcherFunction of watchers) {
const listen = () => {
watcherFunction(store.getState());
};
// Call once as the app initializes.
listen();
// Call every time the store changes.
store.subscribe(listen);
}
}
Example #2
Source File: Tools.ts From foundation-lib-spa-core with Apache License 2.0 | 6 votes |
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 #3
Source File: Context.ts From foundation-lib-spa-core with Apache License 2.0 | 5 votes |
export function useStore() : EnhancedStore {
return useEpiserver().getStore();
}
Example #4
Source File: Spa.ts From foundation-lib-spa-core with Apache License 2.0 | 5 votes |
public getStore(): EnhancedStore {
this.enforceInitialized();
return this._state;
}
Example #5
Source File: Spa.ts From foundation-lib-spa-core with Apache License 2.0 | 5 votes |
protected _state!: EnhancedStore;
Example #6
Source File: Tools.ts From foundation-lib-spa-core with Apache License 2.0 | 5 votes |
export function setLanguage<S = any, A extends Action<any> = AnyAction, M extends Middlewares<S> = Middlewares<S>>(newLanguage: string, store: EnhancedStore<S,A,M>) : void
{
const action : A = {
type: 'OptiContentCloud/SetState',
currentLanguage: newLanguage
} as unknown as A;
store.dispatch(action);
}
Example #7
Source File: Context.d.ts From foundation-lib-spa-core with Apache License 2.0 | 5 votes |
export declare function useStore(): EnhancedStore;
Example #8
Source File: Spa.d.ts From foundation-lib-spa-core with Apache License 2.0 | 5 votes |
getStore(): EnhancedStore;
Example #9
Source File: Spa.d.ts From foundation-lib-spa-core with Apache License 2.0 | 5 votes |
protected _state: EnhancedStore;
Example #10
Source File: Tools.d.ts From foundation-lib-spa-core with Apache License 2.0 | 5 votes |
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;
Example #11
Source File: Tools.d.ts From foundation-lib-spa-core with Apache License 2.0 | 5 votes |
export declare function setLanguage<S = any, A extends Action<any> = AnyAction, M extends Middlewares<S> = Middlewares<S>>(newLanguage: string, store: EnhancedStore<S, A, M>): void;
Example #12
Source File: reduxTester.ts From grafana-chinese with Apache License 2.0 | 4 votes |
reduxTester = <State>(args?: ReduxTesterArguments<State>): ReduxTesterGiven<State> => {
const dispatchedActions: AnyAction[] = [];
const logActionsMiddleWare: Middleware<{}, Partial<StoreState>> = (
store: MiddlewareAPI<Dispatch, Partial<StoreState>>
) => (next: Dispatch) => (action: AnyAction) => {
// filter out thunk actions
if (action && typeof action !== 'function') {
dispatchedActions.push(action);
}
return next(action);
};
const preloadedState = args?.preloadedState ?? (({} as unknown) as State);
const debug = args?.debug ?? false;
let store: EnhancedStore<State> | null = null;
const givenRootReducer = (rootReducer: Reducer<State>): ReduxTesterWhen<State> => {
store = configureStore<State>({
reducer: rootReducer,
middleware: [logActionsMiddleWare, thunk],
preloadedState,
});
setStore(store as any);
return instance;
};
const whenActionIsDispatched = (
action: any,
clearPreviousActions?: boolean
): ReduxTesterWhen<State> & ReduxTesterThen<State> => {
if (clearPreviousActions) {
dispatchedActions.length = 0;
}
store.dispatch(action);
return instance;
};
const whenAsyncActionIsDispatched = async (
action: any,
clearPreviousActions?: boolean
): Promise<ReduxTesterWhen<State> & ReduxTesterThen<State>> => {
if (clearPreviousActions) {
dispatchedActions.length = 0;
}
await store.dispatch(action);
return instance;
};
const thenDispatchedActionShouldEqual = (...actions: AnyAction[]): ReduxTesterWhen<State> => {
if (debug) {
console.log('Dispatched Actions', JSON.stringify(dispatchedActions, null, 2));
}
if (!actions.length) {
throw new Error('thenDispatchedActionShouldEqual has to be called with at least one action');
}
expect(dispatchedActions).toEqual(actions);
return instance;
};
const thenDispatchedActionPredicateShouldEqual = (
predicate: (dispatchedActions: AnyAction[]) => boolean
): ReduxTesterWhen<State> => {
if (debug) {
console.log('Dispatched Actions', JSON.stringify(dispatchedActions, null, 2));
}
expect(predicate(dispatchedActions)).toBe(true);
return instance;
};
const instance = {
givenRootReducer,
whenActionIsDispatched,
whenAsyncActionIsDispatched,
thenDispatchedActionShouldEqual,
thenDispatchedActionPredicateShouldEqual,
};
return instance;
}