Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass value with bindActionCreators?

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.

like image 848
Simon Breton Avatar asked Sep 09 '16 17:09

Simon Breton


People also ask

How do you use bindActionCreators?

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.

What are the two parameters of bindActionCreators?

bindActionCreators accepts two parameters: A function (an action creator) or an object (each field an action creator) dispatch.


2 Answers

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);
}
like image 180
vijayst Avatar answered Sep 16 '22 18:09

vijayst


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

like image 44
namhoaingo Avatar answered Sep 18 '22 18:09

namhoaingo