【问题标题】:mapDispatchToProps isn't connecting function to propsmapDispatchToProps 没有将函数连接到道具
【发布时间】:2019-09-15 09:48:27
【问题描述】:

我正在使用带有 typescript 的 React-Redux,并试图访问 mapDispatchToProps 中定义的函数,但是我收到以下错误:

未捕获的类型错误:this.props.getStoreList 不是函数 在 t.componentDidMount

涉及的两个文件是container.tsx 和mapper.ts。我已经尝试将 mapper.ts 的内容放入 container.tsx 以防导入出现问题,但这并不能解决错误。

该场景与之前的 stackoverflow 问题非常相似:mapDispatchToProps is not putting my function into props 但是该解决方案似乎不适用于我的情况。

container.tsx

import * as React from 'react';
import { connect } from "react-redux";
import { mapStateToProps, mapDispatchToProps } from "./mapper";
import { IStoreListComponentProps } from './component';

export interface IStoreListContainerProps extends IStoreListComponentProps {
    fetchStoreList?: () => void;
}

export class StoreListContainer extends React.Component<IStoreListContainerProps> {
    componentDidMount() {
        this.props.fetchStoreList();
    }

    render() {
        return <div>Example</div>;
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(StoreListContainer);

mapper.ts

import { fetchStoreList } from "./actions/fetch-store-list";
import { IState } from "../../features/store/model";

export const mapDispatchToProps = (dispatch: any) => {
    return {
        getStoreList: () => { dispatch(fetchStoreList()); },
    };
};

export const mapStateToProps = (state: IState) => {
    return {
        storeList: state.storeList
    };
};

感谢您的帮助!

【问题讨论】:

  • "fetchStoreList" !== "getStoreList"

标签: reactjs typescript react-redux


【解决方案1】:
export const mapDispatchToProps = (dispatch: any) => {
    return {
        getStoreList: () => dispatch(fetchStoreList()),
    };
};

试着这样写

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 2017-05-30
    • 2019-09-12
    • 1970-01-01
    • 2020-04-27
    • 2018-04-05
    • 2018-01-09
    相关资源
    最近更新 更多