gpt4 book ai didi

typescript - 如何在带有 TypeScript 的 redux 应用程序中使用选择器?

转载 作者:行者123 更新时间:2023-12-03 13:55:14 24 4
gpt4 key购买 nike

我尝试使用 selectors从我的 Redux 应用程序中重新选择库。

我的选择器文件看起来:

import { createSelector } from 'reselect'

const postsSelectors = state => state.global.posts.allPostse;

export const getPosts = createSelector(
[ postsSelectors ],
(posts) => posts
);

然后我尝试在我的组件中使用,如下所示:
const mapStateToProps = (state) => ({
posts: getPosts(state),
});

当我尝试编译所有这些时,出现以下错误:

enter image description here

我猜我是如何为 props 声明类型的,目前看起来是这样的:
interface Props{
posts(state: any): any
loadStories(): void;
};

请帮我解决这个问题。提前致谢。

最佳答案

更多的例子 类型 :

  • 描述类型

  • type TPostData = {
    type: string;
    };

    type TPostsState = TPostData[];

    type TState = {
    posts: TPostsState;
    };
  • 创建选择器

  • // get all posts
    export const selectPosts = (state: TState): TPostsState => state.posts;

    // get new posts
    export const selectNewPosts = createSelector<
    TState,
    TPostsState,
    TPostData[]>(
    selectPosts,
    (posts) => posts.filter(({ type }) => type === 'new'),
    );

    结果:您已获得所有类型参数为“new”的帖子。

    关于typescript - 如何在带有 TypeScript 的 redux 应用程序中使用选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42283729/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com