简体   繁体   中英

Chaining redux connect mapStateToProps , to access props inside mapDispatchToProps

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 ?

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM