【发布时间】:2018-08-07 09:13:51
【问题描述】:
在我的 React + Redux 应用程序中,我正在尝试使用 mapDispatchToProps 实用程序,但是每当我将其放入 connect(mapStateToProps, mapDispatchToProps) 时,它都会给我一个错误提示 Uncaught TypeError: dispatch is not a function at new ReduxApp (ReduxApp.js:42)
这可能是什么问题? PS:下面是文件
ReduxApp.js
import React from 'react';
import { Router, Route } from 'react-router-dom';
import { connect } from 'react-redux';
import { history } from './_helpers';
import { alertActions } from './_actions'
class ReduxApp extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
const { dispatch } = this.props;
dispatch(alertActions.success("hello world"));
}
handleChange(){
this.props.dispatch(alertActions.clear());
}
render(){
const { alert } = this.props;
return(
<div>
<h1>{alert.message}</h1>
<button onClick={this.handleChange}>clear</button> {/* this is working since this function is declared outside the mapDispatchToProps. */}
<button onClick={this.props.handleClick}>clear</button>
</div>
);
}
}
const mapStateToProps = (state) => ({
alert : state.alert
});
const mapDispatchToProps = (dispatch) => ({
handleClick: () => dispatch(alertActions.clear())
});
const connectedApp = connect(mapStateToProps, mapDispatchToProps)(ReduxApp); // when I add mapDispatchToProps in the connect(), I get thhe issue.
export { connectedApp as ReduxApp }
【问题讨论】:
标签: javascript reactjs react-native redux