store: add a useAsyncDispatch function for type-correct async dispatch.

master
Tom Hacohen 4 years ago
parent a7cae3203b
commit 2f8794e8fd

@ -5,6 +5,8 @@ import { createStore, applyMiddleware } from "redux";
import { persistStore } from "redux-persist"; import { persistStore } from "redux-persist";
import thunkMiddleware from "redux-thunk"; import thunkMiddleware from "redux-thunk";
import { createLogger } from "redux-logger"; import { createLogger } from "redux-logger";
import { ActionMeta } from "redux-actions";
import { useDispatch } from "react-redux";
import promiseMiddleware from "./promise-middleware"; import promiseMiddleware from "./promise-middleware";
@ -23,6 +25,13 @@ if (process.env.NODE_ENV === "development") {
middleware.push(createLogger()); middleware.push(createLogger());
} }
export function useAsyncDispatch() {
const dispatch = useDispatch();
return function asyncDispatch<T, V>(action: ActionMeta<Promise<T>, V>): Promise<ActionMeta<T, V>> {
return dispatch(action) as any;
};
}
export const store = createStore( export const store = createStore(
reducers, reducers,
applyMiddleware(...middleware) applyMiddleware(...middleware)

Loading…
Cancel
Save