This.props.dispatch not a function - React-Redux
This.props.dispatch not a function - React-Redux
我正在尝试从智能组件中调度操作。我尝试使用mapDispatchToProps
和this.props.dispatch(actions.getApplications(1))
但都没有将操作绑定到props
。
我不确定是否是因为不包括我的mapStateToProps
?我试图包含它,但它也没有用。
感谢任何帮助,对于下面代码块的长度,我深表歉意。
import classNames from 'classnames';
import SidebarMixin from 'global/jsx/sidebar_component';
import Header from 'common/header';
import Sidebar from 'common/sidebar';
import Footer from 'common/footer';
import AppCard from 'routes/components/appCard';
import { getApplications } from 'redux/actions/appActions';
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import actions from 'redux/actions';
import { VisibilityFilters } from 'redux/actions/actionTypes';
class ApplicationContainer extends React.Component {
constructor(props){
super(props)
this.state = {
applicants: []
}
this.onDbLoad = this.onDbLoad.bind(this)
}
onDbLoad(){
console.log(this.props.dispatch)
// this.props.getApplications(1)
}
render() {
return (
<Col sm={12} md={4} lg={4}>
<PanelContainer style={panelStyle}>
<Panel>
<PanelBody >
<Grid>
<Row>
{ this.onDbLoad() }
</Row>
</Grid>
</PanelBody>
</Panel>
</PanelContainer>
</Col>
)}
}
function mapDispatchToProps(dispatch){
return bindActionCreators({ getApplications: getApplications },dispatch)
}
export default connect(null, mapDispatchToProps)(ApplicationContainer);
@SidebarMixin
export default class extends React.Component {
render() {
const app = ['Some text', 'More Text', 'Even More Text'];
var classes = classNames({
'container-open': this.props.open
})
return (
<Container id='container' className={classes}>
<Sidebar />
<Header />
<Container id='body'>
<Grid>
<Row>
<ApplicationContainer />
</Row>
</Grid>
</Container>
<Footer />
</Container>
)}
}
根据此处的 Redux 常见问题解答问题,如果您不提供自己的 mapDispatchToProps
函数,默认情况下可以使用 this.props.dispatch
。 如果您确实提供了mapDispatchToProps
函数,则您负责自己返回一个名为dispatch
的道具:
const mapDispatchToProps = dispatch => ({
...other methods,
dispatch // ← Add this
})
export default connect(null, mapDispatchToProps)(Component)
或者,您可以确保操作创建者使用 Redux 的 bindActionCreators
实用程序预先绑定,而不必担心在组件中使用this.props.dispatch
。
正如您在问题中提到的,mapDispatchToProps
应该是connect
的第二个参数。
如果您没有任何状态映射要做,则可以传递null
作为第一个参数:
export default connect(null, mapDispatchToProps)(ApplicationContainer);
执行此操作后,this.props.getApplications
将被绑定。
根据您的评论,如果您想访问this.props.dispatch
而不是绑定操作,只需调用connect()
而不传递任何映射器,默认行为将注入dispatch
。
如果要同时拥有绑定动作创建者和this.props.dispatch
,则需要向传递给mapDispatchToProps
的对象添加dispatch
。像dispatch: action => action
.或者,由于您没有将所有内容都放在根键下(如 actions
),您可以执行以下操作:
function mapDispatchToProps(dispatch) {
let actions = bindActionCreators({ getApplications });
return { ...actions, dispatch };
}
- React Redux错误页面管理
- React redux初始化功能,无论状态变化如何
- 为什么react/redux前端项目在package.json中包含生产依赖项
- React+Redux性能优化与组件ShouldUpdate
- React Redux无法让减速器拾取动作
- 类叶组件中的connect()是react+redux中反模式的标志吗
- React/redux应用程序在Safari上呈现空白屏幕
- Universal React/redux未在状态更新时呈现
- React+Redux-期望reducer是一个函数
- React+Redux-未捕获错误:操作可能没有未定义的“;类型“;所有物
- This.props.dispatch not a function - React-Redux
- 在 React/Redux 应用程序中,我应该何时显式传递一个 prop,而不是使用 mapStateToProps 从
- 在 react-redux 中连接时出错
- 我在哪里从 React Redux 应用程序中的服务器获取初始数据
- React/Redux 状态在第二次操作调用时为空
- 将 react-redux 与基于事件的第三方库一起使用的最佳方法是什么?
- 有没有更好的方法来处理窗口属性&React/Redux中的子组件
- 如何调整大小,然后用react redux渲染到画布上
- 在 React/Redux 中管理同级组件之间的滚动状态
- React Redux-Dispatch检索输入值