Skip to content

Instantly share code, notes, and snippets.

View mdeljavan's full-sized avatar

Mohammadreza Deljavan mdeljavan

View GitHub Profile
@mdeljavan
mdeljavan / readme.md
Created April 27, 2019 11:58 — forked from astoilkov/readme.md
Async Operations with useReducer Hook

Async Operations with useReducer Hook

We were discussing with @erusev what we can do with async operation when using useReducer() in our application. Our app is simple and we don't want to use a state management library. All our requirements are satisfied with using one root useReducer(). The problem we are facing and don't know how to solve is async operations.

In a discussion with Dan Abramov he recommends Solution 3 but points out that things are fresh with hooks and there could be better ways of handling the problem.

Problem

Doing asynchronous operations in a useReducer reducer is not possible. We have thought of three possible solutions and can't figure which one is better or if there is an even better solution.

@mdeljavan
mdeljavan / redux-actions.ts
Created June 19, 2018 07:32 — forked from milankorsos/redux-actions.ts
Correct TypeScript typing example for Redux Thunk actions
import {Action, ActionCreator, Dispatch} from 'redux';
import {ThunkAction} from 'redux-thunk';
// Redux action
const reduxAction: ActionCreator<Action> = (text: string) => {
return {
type: SET_TEXT,
text
};
};