esamattis / immer-reducer

Type-safe and terse reducers with Typescript for React Hooks and Redux
http://npm.im/immer-reducer
MIT License
224 stars 15 forks source link

Asynchronous methods #10

Closed samjmck closed 5 years ago

samjmck commented 5 years ago

Was wondering if there was a way to implement asynchronous methods using thunk?

esamattis commented 5 years ago

Not really since immer-reducer is basically just a way to define the reducer function.

This is

class Reducer extends ImmerReducer {
    setName(name) {
        this.draftState.name = name;
    }
}

basically same as

function reducer(action, state) {
  switch (action.name) {
    case "SET_NAME":
      return {...state, name: action.name};
    default:
      return state;
  }
}

In theory it could be possible to make some exception for createActionCreators() that generates thunks or something but async actions are whole new can of worms and I like that immer-reducer focuses doing only thing well.

That being said immer-reducer works well with the redux-thunk module. I've also heard people using this with redux-observable. I personally use this thunk creating wrapper which helps with TS typing.

samjmck commented 5 years ago

Ah right, I understand now. Thanks!