Skip to content

Change position of extra arguments from withExtraArgument #111

@smeijer

Description

@smeijer

I love to use the feature of thunk.withExtraArgument to pass along a context to my thunks. For example an API or in my case meteor / mongo collections.

However what's confusing me, and to be honest doesn't look very nice in my code. Is that redux-thunk has a different signature than redux-saga regarding to passing along extra arguments.

Redux-saga supports passing along extra arguments when starting the sagas trough: sagaMiddleware.run(saga, context). I'm not talking about the initialization code, just adding that snippet to add some context.

// store init code
const context = { fetch: () => ({ data: 'foobar' }) };
thunkMiddleware.withExtraArgument(context)
sagaMiddleware.run(saga, context);

What I'm talking about is the next two signatures. One of a thunk and a saga.

// thunk signature
export function (payload) {
  return function(dispatch, getState, context) {
    const { data } = context.fetch(payload);
    dispatch({ type: 'FETCH_SUCCESS', payload: data });
  }
}
// saga signature
export function* saga(context, { payload }) {
  const { data } = yield call(context.fetch, payload);
  yield put({ type: 'FETCH_SUCCESS', payload: data });
}

Where redux-thunk adds context as last argument to the thunk, redux-saga adds it as first argument to the saga. This difference is somewhat error-prone.

Is it possible to change the behavior of withExtraArgument to add the context as first argument? I guess it shouldn't be that difficult to make it configurable, due to the decision to only support a single extra argument, as written here: #70 (comment)

It's not that it is a big issue, or a deal-braker or anything like that. But it can be confusing sometimes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions