0

In order to access to the props of the component inside the mapDispatchToProps i chained my connect like so

export default connect(mapStateToProps, null)(connect(mapStateToProps, mapDispatchToProps)(MyComponent));

And then i manage to access to the props inside the mapDispatchToProps like so

const mapDispatchToProps = (dispatch,ownProps) => {

    const myProp = ownProps.theProp

}

Is it a bad things to do ? Any alternative exists ?

Loic H
  • 328
  • 4
  • 12
  • you just want to combine multiple `mapStateToProps`? – Icepickle May 15 '19 at 21:11
  • I added a precision, indeed I want to access the props in the mapStateToProps, by doing this it works perfectly , but i wonder myself if this is a good thing to do and of course if any other way exists – Loic H May 15 '19 at 21:14
  • im having a difficult time understanding why this is necessary, is it because your selector in `mapDispatchToProps` requires existing props as arguments? – WilliamNHarvey May 15 '19 at 21:16
  • I think i found some clues here : https://github.com/reduxjs/redux-devtools/issues/250 – Loic H May 15 '19 at 21:28

2 Answers2

2

Is it a bad things to do?

IMO, It is certainly bad. connect() is an HOC. connect(...)(connect(...)(MyComponent)) is redundant.

Any alternative exists ?

Use mergeProps instead or break the components properly and use redux-saga to use a common interaction point (the redux store).

Joseph D.
  • 11,804
  • 3
  • 34
  • 67
0

The correct way to connect mapDispatch to props and mapStateToProps is like this:

export default connect(mapStateToProps, mapDispatchToProps)(MetaDataTaggingApp);

Also I don't think you should have to access props in mapDispatchToProps. mapDispatchToProps is basically just telling your component which dispatch actions it's allowed to use.

const mapDispatchToProps = {
  aReduxAction,
  anotherReduxAction
}

If you need to pass props into these dispatches, it should be when you are invoking them.

cullanrocks
  • 457
  • 4
  • 16
  • You absolutely do have access to ownProps in mapDispatchToProps, it's right in the documentation https://react-redux.js.org/using-react-redux/connect-mapdispatch#arguments – WilliamNHarvey May 15 '19 at 21:17
  • Ah I guess I've just never had to use ownProps. I've always made mapDispatchToProps an object. – cullanrocks May 15 '19 at 21:20
  • Sending props at invocation is usually still better, but I'm not really sure if that's why the question asker wanted them? This is probably right though. The action they're dispatching should just take an argument – WilliamNHarvey May 15 '19 at 21:24
  • I have an empty object ownProps returned in my console by doing this : ` const mapDispatchToProps = (dispatch,ownProps) => { console.log(ownProps)` – Loic H May 15 '19 at 21:26
  • 1
    Maybe this will help OP: https://stackoverflow.com/questions/41198842/what-is-the-use-of-the-ownprops-arg-in-mapstatetoprops-and-mapdispatchtoprops – cullanrocks May 15 '19 at 21:28
  • I found some answers here too : https://github.com/reduxjs/redux-devtools/issues/250 – Loic H May 15 '19 at 21:29