Skip to content

Instantly share code, notes, and snippets.

@FMCorz
Created February 1, 2019 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FMCorz/70aa279ab83d2b8a5432ddaa078112c0 to your computer and use it in GitHub Desktop.
Save FMCorz/70aa279ab83d2b8a5432ddaa078112c0 to your computer and use it in GitHub Desktop.
Redux: Accessing state from mapDispatchToProps
// Re. https://github.com/reduxjs/react-redux/issues/535
//
// Here is a solution to access props derived from the state in mapDispatchToProps.
//
// I personally prefer this over those suggested in the issue above.
const unlockItem = () => null; // Replace with some redux action.
const Unlocker = props => {
return <button onClick={() => props.unlockItem(props.itemId)}>Unlock</button>;
};
const QuotaInjector = props => {
return <Unlocker {...props} unlockItem={itemId => props.unlockItem(itemId, props.quota)} />;
};
const ConnectedUnlocker = connect(
state => ({
quota: state.quota
}),
dispatch => ({
unlockItem: (itemId, quota) => {
if (quota > 0) {
dispatch(unlockItem(itemId));
}
}
})
)(QuotaInjector);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment