Simon Breton
Simon Breton

Reputation: 2876

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.

Upvotes: 6

Views: 3160

Answers (3)

VahidKK
VahidKK

Reputation: 444

If you want to use bindActionCreators , you should write it like this :

function mapDispatchToProps(dispatch) {
  return {
    fetchgadata: bindActionCreators((event) => fetchgadata(event.target.value), dispatch),
  };
};

Upvotes: 0

namhoaingo
namhoaingo

Reputation: 103

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

Upvotes: 3

vijayst
vijayst

Reputation: 21914

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);
}

Upvotes: 4

Related Questions