简体   繁体   English

链接redux连接mapStateToProps,以访问mapDispatchToProps内部的道具

[英]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 为了访问mapDispatchToProps内部的组件的props ,我像这样将我的连接链接mapDispatchToProps

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

And then i manage to access to the props inside the mapDispatchToProps like so 然后我设法像这样访问mapDispatchToProps内部的道具

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()是一个HOC。 connect(...)(connect(...)(MyComponent)) is redundant. connect(...)(connect(...)(MyComponent))是多余的。

Any alternative exists ? 有其他选择吗?

Use mergeProps instead or break the components properly and use redux-saga to use a common interaction point (the redux store). 请改用mergeProps或适当地破坏组件,并使用redux-saga使用公共交互点(redux存储)。

The correct way to connect mapDispatch to props and mapStateToProps is like this: 将mapDispatch连接到props和mapStateToProps的正确方法是这样的:

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

Also I don't think you should have to access props in mapDispatchToProps. 另外,我认为您不必在mapDispatchToProps中访问道具。 mapDispatchToProps is basically just telling your component which dispatch actions it's allowed to use. mapDispatchToProps基本上只是告诉您的组件允许使用哪些调度动作。

const mapDispatchToProps = {
  aReduxAction,
  anotherReduxAction
}

If you need to pass props into these dispatches, it should be when you are invoking them. 如果您需要将道具传递到这些调度中,那应该是在调用它们时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Redux连接在初始加载时未调用mapStateToProps或mapDispatchToProps - Redux connect not calling mapStateToProps or mapDispatchToProps on initial load react redux:当使用Connect for mapStateToProps和mapDispatchToProps时仍然为this.props获取空对象 - react redux: still getting empty object for this.props when using Connect for mapStateToProps and mapDispatchToProps 链接connect / mapStateToProps / mapDispatchToProps函数以在react-redux中重用代码 - Chain connect/mapStateToProps/mapDispatchToProps functions for code reuse in react-redux mapStateToProps 和 mapDispatchToProps 没有向组件发送道具 - mapStateToProps and mapDispatchToProps is not sending props to component 在Redux mapDispatchToProps中使用组件状态或道具 - Working with component state or props inside Redux mapDispatchToProps 如何使用react-redux连接mapStateToProps,MapDispatchToProps和redux-router - How use react-redux connect with mapStateToProps,MapDispatchToProps and redux-router 我可以在Redux中没有mapStateToProps mapDispatchToProps吗? - Can I mapDispatchToProps without mapStateToProps in Redux? 了解 React-Redux 中的 mapStateToProps 和 mapDispatchToProps - Understanding mapStateToProps & mapDispatchToProps in React-Redux 在没有任何mapstatetoprops的情况下在redux中连接,并且仍然将其作为props - connect in redux without any mapstatetoprops and still getting things as props 每次要访问 redux 中的 state 时,是否必须声明 mapDispatchToProps 和 mapStateToProps? - Do you have to declare mapDispatchToProps and mapStateToProps everytime you want to access the state in redux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM