typeScript react -redux

时间:2017-09-06 07:11:56

标签: reactjs typescript redux

我想使用带有typeScript的React-Redux来操作动作,但是当我使用mapDisPatchToProps()时,我不知道如何定义dispatch的类型,还有我的代码:

这是组件文件:

    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

那么,如何解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

像这样

import { Dispatch } from 'redux';
/*other code*/
const mapDispatchToProps = (dispatch: Dispatch<object>) => ({
  asyncRequest: (name: string) => dispatch(someAction(name)),
  otherAction: () => dispatch(someAction())
});