Passing State to Action Creators
See original GitHub issueIn 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:
- Created 8 years ago
- Reactions:9
- Comments:6 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
is not the second parameter provided when using redux-thunk the getState function?
@tomascharad : I believe Redux-Saga provides a
select()
operation, which you would use roughly like: