Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access state in createAsyncAction #244

Open
NewBieBR opened this issue Nov 24, 2020 · 1 comment
Open

Access state in createAsyncAction #244

NewBieBR opened this issue Nov 24, 2020 · 1 comment

Comments

@NewBieBR
Copy link

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)

  • redux-thunk doesn't seem to be compatible with createAsyncAction
  • Another solution is to export store and access to the state directly with it.

Additional context (optional)

@kylegoetz
Copy link

how about

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants