Created
February 1, 2019 07:09
-
-
Save FMCorz/70aa279ab83d2b8a5432ddaa078112c0 to your computer and use it in GitHub Desktop.
Redux: Accessing state from mapDispatchToProps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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