Skip to content

Instantly share code, notes, and snippets.

@DimitryDushkin
Last active October 5, 2018 21:30
Show Gist options
  • Save DimitryDushkin/b454d40f64339de139aa22bc480e0720 to your computer and use it in GitHub Desktop.
Save DimitryDushkin/b454d40f64339de139aa22bc480e0720 to your computer and use it in GitHub Desktop.
Typescript + redux + redux-thunk + react-navigation typings example
import {Action as ReduxAction, Middleware as ReduxMiddleware, Store as ReduxStore} from 'redux';
import {NavigationState, NavigationScreenProp} from 'react-navigation';
import {ThunkAction, ThunkDispatch} from 'redux-thunk';
import {StateType} from 'typesafe-actions';
import {reducers} from 'store/reducers/root.reducer';
export type AppState = StateType<typeof reducers>;
export interface Action<T = any, P extends object = any, M = any> extends ReduxAction<T> {
payload?: P;
meta?: M | null;
error?: boolean;
}
export type Thunk<R = any> = ThunkAction<R, AppState, undefined, Action>;
export type Dispatch = ThunkDispatch<AppState, undefined, Action>;
export type Store = ReduxStore<AppState, Action>;
export type Middleware = ReduxMiddleware<undefined, AppState, Dispatch>;
export type NavigationProp = NavigationScreenProp<NavigationState>;
// How to use
// 1. Some connected component
//...
export default connect((state: AppState) => ({ bar: state.foo.bar })(SomeComponent);
// 2. Action creators
export const feedRequestInitialItems = (): Thunk<Promise<void>> => (dispatch, getState) => {
dispatch({...});
return Promise.resolve();
}
// 3. Middlewares
const metricaMiddleware: Middleware = ({dispatch, getState}) => {
return (next) => (action) => next(action);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment