打字稿反应-redux
问题描述:
我想使用做出反应,终极版与打字稿到dipatch的动作,但是当我使用mapDisPatchToProps()
,我不知道如何定义的dispatch
类型,有我的代码:打字稿反应-redux
这是组件文件:
import * as React from 'react';
import Content from '../component/content';
interface Props {
allCityInformation: {'cityName': string, 'img': string}[];
onGetAllCityInformation: Function;
}
class HomePage extends React.Component<Props> {
componentDidMount() {
this.props.onGetAllCityInformation();
}
render() {
return (
<div>
<Content/>
</div>
);
}
}
export default HomePage;
这是我的容器文件:
import { connect } from 'react-redux';
import HomePage from '../pages/homePage';
export type DispatchGetCityInformation =() => void;
const mapDispatchToProps = (dispatch: DispatchGetCityInformation) => {
return{
onGetAllCityInformation: dispatch(getCityInformation())
};
};
export default connect(() => {return {};
}, mapDispatchToProps)(HomePage);
现在,该错误信息是: enter image description here
那么,如何解决这个问题呢?
答
这样
import { Dispatch } from 'redux';
/*other code*/
const mapDispatchToProps = (dispatch: Dispatch<object>) => ({
asyncRequest: (name: string) => dispatch(someAction(name)),
otherAction:() => dispatch(someAction())
});
检查这个话题:https://stackoverflow.com/questions/46055018/shorter-way-to-mapdispatchtoprops-using-react-redux-and-typescript – niba