piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture
https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox
MIT License
2.41k stars 99 forks source link

Access state in createAsyncAction #244

Open NewBieBR opened 3 years ago

NewBieBR commented 3 years ago

Is your feature request related to a real problem or use-case?

Yes. I want to access to the state in my action creator in order to inject a jwt in my actions. This use case is pretty common if your app uses an API that require an authorization token (app like facebook or instagram). Instead of getting the token and dispatching it in the action manually:

const authToken = useSelector(state => state.auth.authToken);
dispatch(fetchUserInfoAsync.request(authToken));

Describe a solution including usage in code example

It would be better if the authToken can be injected in the action creator:

// actions.ts

const fetchUserInfoAsync = createAsync(['FETCH_USER_INFO', (state) => () => {authToken: state.auth.authToken}],...)

// Component.tsx
dispatch(fetchUserInfoAsync.request());

Who does this impact? Who is this for?

People using an API that require an authorization token

Describe alternatives you've considered (optional)

Additional context (optional)

kylegoetz commented 3 years ago

how about

const actionFactory = (authToken) => ({
  return {
    fetchUserInfoAsync = createAsync(['FETCH_USER_INFO', ...)
  }
})