Until now I've been dealing with two kind of mapDispatchToProps
:
One :
function mapDispatchToProps(dispatch) {
return {
fetchgadata: bindActionCreators(fetchgadata, dispatch)
};
}
Two :
const mapDispatchToProps = (dispatch) => {
return {
onTimeChange: (e) => {dispatch (onSetTimeRange(e.target.value));},
};
};
What I would like to do is using e.target.value
with bindActionCreators
. So How should I refactor the first mapDispatchToProps
above if I want to include e.target.value
inside and pass this value to my fetchgadata
action creator ?
I'm learning so I hope my question is clear. Please help me to improve it if not ! Thanks.
bindActionCreators(actionCreators, dispatch) Turns an object whose values are action creators, into an object with the same keys, but with every action creator wrapped into a dispatch call so they may be invoked directly. Normally you should just call dispatch directly on your Store instance.
bindActionCreators accepts two parameters: A function (an action creator) or an object (each field an action creator) dispatch.
Instead of passing the event args, pass in e.target.value. That value will be the first argument in the action creator. Component code should be:
handleSubmit(e) {
this.props.onTimeChange(e.target.value);
}
You can try the following:
function mapDispatchToProps(dispatch) {
return {
bindActionCreators({
fetchgadata: function(e) {
dispatch(onSetTimeRange(e.target.value));
}
}, dispatch)
};
}
If you want more function, you can simplify add to the bottom and keep chaining
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With