question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing State to Action Creators

See original GitHub issue

In the case where you need to read state in an action creator what is the best way to do so… For example suppose I have an action creator that gets a new user object:

export function load(state) {
  return (dispatch) => {
    if (getUser(state)) return null;

    get()
      .then((response) => {
        dispatch({
          type: AppConstants.USER_LOAD_SUCCESS,
          payload: {
            response
          }
        });
      });
  }
} 

Notice it is configured ahead of time with the current state, this enables the action creator to decide if it should really fire the action or not. Currently the mapping state and actions to components is separated, what do you do when an action creator needs access to state?

export default connect((state, props) => {
  return {
    user: getUser(state)
  };
}, (dispatch) => {
  // I want state here to configure my action creator!
  return {
    load: () => dispatch(load(state))
  }
})(User);

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:9
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

90reactions
mikeyamadeocommented, Sep 11, 2015
export function load() {
  return (dispatch, getState) => {
    const state = getState()
    if (getUser(state)) return null;

    get()
      .then((response) => {
        dispatch({
          type: AppConstants.USER_LOAD_SUCCESS,
          payload: {
            response
          }
        });
      });
  }
} 

is not the second parameter provided when using redux-thunk the getState function?

4reactions
markeriksoncommented, Jul 18, 2016

@tomascharad : I believe Redux-Saga provides a select() operation, which you would use roughly like:

const allTheState = yield select(state => state);
const oneSmallPieceOfState = yield select(someSpecificSelectorFunction);
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - How do I pass state into my action creator - Stack Overflow
The proper way to do it is via setState since the updates my be asynchronous. You may check out this link. How can...
Read more >
bindActionCreators - Redux
The only use case for bindActionCreators is when you want to pass some action creators down to a component that isn't aware of...
Read more >
Actions and reducers: updating state - Human Redux
In Redux all changes to the application state happen via an "action. ... But we can only pass Redux a single reducer when...
Read more >
Redux: Extracting Action Creators | egghead.io
Redux: Describing State Changes with Actions ... now extract that to add toggle todo action creator, to which I pass the ID of...
Read more >
Action Creators - Redux Form
However, it is recommended that you use the actions passed as props to your component for most of your needs, as they are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found