【问题标题】:Redux ConnectedProps is of type neverRedux ConnectedProps 的类型是 never
【发布时间】:2020-11-20 13:04:46
【问题描述】:

我正在尝试使用 official documentation guide 将组件连接到我的 Redux 存储,但连接的道具似乎是 never 类型。

到目前为止,这是我的代码:

类型定义:

export interface CardInterface {
    id: string;
    titulo: string;
    descripcion: string;
    url?: string;
    fecha: Date;
}

export type CardsState = Readonly<{
    cards: CardInterface[];
}>;

实际组件:

import * as React from 'react';
import { connect, ConnectedProps } from 'react-redux';

const mapStateToProps = (state: CardsState) => ({
    cards: state.cards,
});

const connector = connect(mapStateToProps, null);

type Props = ConnectedProps<typeof connector>;

const CardList: React.FC<Props> = ({ cards }) => {
    return (
        <div className="grid-container container">
            {cards.map((card: CardInterface) => (
                <Card
                    title={card.titulo}
                    description={card.descripcion}
                    image={card.url}
                />
            ))}
        </div>
    );
};

export default connector(CardList);

当我尝试遍历卡片时,linter 将 cards 属性识别为 never

还有其他人遇到过这个问题吗?非常感谢。

【问题讨论】:

    标签: reactjs typescript redux react-redux


    【解决方案1】:

    省略未使用的 mapDispatchToProps 参数应该可以修复这个 Typescript 错误:

    const connector = connect(mapStateToProps); //instead of passing null 
    

    显然是known issue

    【讨论】:

    • 该死,其实就是这么简单。非常感谢!
    • 像魅力一样工作!
    猜你喜欢
    • 2020-07-11
    • 2018-04-04
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    相关资源
    最近更新 更多