Skip to content

Instantly share code, notes, and snippets.

@Nio-o
Forked from milankorsos/redux-actions.ts
Created November 30, 2019 23:58
Show Gist options
  • Select an option

  • Save Nio-o/4ca3992fd1cb62c3330bb51eb52b017f to your computer and use it in GitHub Desktop.

Select an option

Save Nio-o/4ca3992fd1cb62c3330bb51eb52b017f to your computer and use it in GitHub Desktop.

Revisions

  1. @milankorsos milankorsos revised this gist Aug 15, 2017. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions redux-actions.ts
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    // Import ActionCreator and Dispatch from react-redux instead of redux
    import {ActionCreator, Dispatch} from 'react-redux';
    import {Action} from 'redux';
    import {Action, ActionCreator, Dispatch} from 'redux';
    import {ThunkAction} from 'redux-thunk';

    // Redux action
  2. @milankorsos milankorsos revised this gist Aug 14, 2017. No changes.
  3. @milankorsos milankorsos revised this gist Aug 14, 2017. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion redux-actions.ts
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    import {Action, ActionCreator, Dispatch} from 'redux';
    // Import ActionCreator and Dispatch from react-redux instead of redux
    import {ActionCreator, Dispatch} from 'react-redux';
    import {Action} from 'redux';
    import {ThunkAction} from 'redux-thunk';

    // Redux action
    const reduxAction: ActionCreator<Action> = (text: string) => {
  4. @milankorsos milankorsos revised this gist Aug 14, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion redux-actions.ts
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,6 @@ const asyncThinkAction: ActionCreator<
    type: SET_TEXT,
    text
    });
    });
    } catch (e) {}
    };
    };
  5. @milankorsos milankorsos revised this gist Aug 14, 2017. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions redux-actions.ts
    Original file line number Diff line number Diff line change
    @@ -24,8 +24,9 @@ const thunkAction: ActionCreator<ThunkAction<Action, IState, void>> = (
    const asyncThinkAction: ActionCreator<
    ThunkAction<Promise<Action>, IState, void>
    > = () => {
    return (dispatch: Dispatch<IState>): Promise<Action> => {
    return Api.call().then(text => {
    return async (dispatch: Dispatch<IState>): Promise<Action> => {
    try {
    const text = await Api.call();
    return dispatch({
    type: SET_TEXT,
    text
  6. @milankorsos milankorsos created this gist Aug 14, 2017.
    35 changes: 35 additions & 0 deletions redux-actions.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import {Action, ActionCreator, Dispatch} from 'redux';

    // Redux action
    const reduxAction: ActionCreator<Action> = (text: string) => {
    return {
    type: SET_TEXT,
    text
    };
    };

    // Redux-Thunk action
    const thunkAction: ActionCreator<ThunkAction<Action, IState, void>> = (
    text: string
    ) => {
    return (dispatch: Dispatch<IState>): Action => {
    return dispatch({
    type: SET_TEXT,
    text
    });
    };
    };

    // Async Redux-Thunk action
    const asyncThinkAction: ActionCreator<
    ThunkAction<Promise<Action>, IState, void>
    > = () => {
    return (dispatch: Dispatch<IState>): Promise<Action> => {
    return Api.call().then(text => {
    return dispatch({
    type: SET_TEXT,
    text
    });
    });
    };
    };