reactjs - 如何在也连接到 Redux 的 Component 中使用 connected-to-Redux-Higher-Order-Component
问题描述
我正在尝试将使用的 a 与也使用的 a 包装起来Container
,从而Redux
导致以下错误:HOC
Redux
TypeError: Object(...) is not a function
> 112 | export default connect(mapStateToProps, mapDispatchToProps) (withRouter(withError(VeggieBuilder)))
我一直试图自己解决这个问题,我猜该解决方案与compose
导出redux
myHOC
或 my Container
.
HOC
我将在此处发布整个内容,以及块container's
下方的导出部分HOC
。
import React, {Fragment} from "react";
import Modal from "../../UI/Modal/Modal";
import { connect } from 'react-redux'
import * as actionCreators from '../../../store/actions/indexActions'
const withError = WrappedComponent => {
return props => {
return<Fragment>
<Modal show={props.error} modalCanceled={() => props.onResetError()}>
{props.error && props.error}
</Modal>
<WrappedComponent {...props} />
</Fragment>
}
}
const mapStateToProps = state => {
return {
error: state.httpReqReducer.errorMessage
}
}
const mapActionsToProps = dispatch => {
return {
onResetError: () => dispatch(actionCreators.resetError())
}
}
export default connect(mapStateToProps, mapActionsToProps)(withError)
现在export
我Container
叫的部分VeggieBuilder
export default connect(mapStateToProps, mapDispatchToProps) (withRouter(withError(VeggieBuilder)))
如果删除我connection
的错误消失Redux
。export
HOC
在此先感谢大家。
如果此处需要任何其他信息,我将相应地编辑问题。
解决方案
你可以这样修改你的 HOC:
import React, { Fragment } from "react";
import Modal from "../../UI/Modal/Modal";
import { connect } from 'react-redux'
import * as actionCreators from '../../../store/actions/indexActions'
const withError = WrappedComponent => {
const wrappedComp = props => {
return <Fragment>
<Modal show={props.error} modalCanceled={() => props.onResetError()}>
{props.error && props.error}
</Modal>
<WrappedComponent {...props} />
</Fragment>
}
const mapStateToProps = state => {
return {
error: state.httpReqReducer.errorMessage
}
}
const mapActionsToProps = dispatch => {
return {
onResetError: () => dispatch(actionCreators.resetError())
}
}
return connect(mapStateToProps, mapActionsToProps)(wrappedComp);
}
export default withError;
调用and将返回一个函数connect
,该函数需要一个反应组件作为参数。mapState
mapDispatch
因此,当您使用export default connect(mapStateToProps, mapActionsToProps)(withError)
note 时,withError
它不是一个反应组件,而是一个返回反应组件的函数。
推荐阅读
- java - Java 向下转换
- reactjs - 如何在没有任何形式的情况下获取 Reacjs 输入文本值
- javascript - Javascript 数组在不希望的情况下得到更新
- c++ - 模板方法访问前向声明的类只能在没有这个指针的情况下编译失败
- python - 如何基于数组在 Python 中为文本文件创建列
- pytorch - 为什么我不能导入 PyTorch?AttributeError:“模块”对象没有属性“inf”
- html - 使用 Userway 可访问应用程序,不阅读选择框
- php - 成功提交后未收到 Paypal 交易详情
- javascript - 盖茨比:在 JavaScript 中定义静态查询?
- sorting - NSMutableDictionary 中的自定义按键排序