gpt4 book ai didi

javascript - 使用选择器获取相关项目

转载 作者:行者123 更新时间:2023-12-01 01:26:04 25 4
gpt4 key购买 nike

我使用 React 和 Redux 来获取帖子,每个帖子都有一个用户。我的标准化数据如下

{
entities: {
posts: {
4: {
name: 'Post 4',
user: 5
}
}
users: {
5: {name: 'User 5'}
}
},
post: {
id: 4
}
}

我正在使用选择器来获取当前帖子

post: getPost(state.entities.posts, 4)


export const getPost = (posts, id) => posts[id];

如何获取此特定帖子的用户名?

我想知道使用选择器获取该帖子的用户信息的好方法是什么。

最佳答案

我将创建两个特定的选择器来检索帖子和用户

const getPost = (posts, postId) => posts[postId]
const getUser = (users, userId) => users[userId]

然后是结合两者的“复杂”选择器

const getPostUser = (state, postId) => {
const userId = getPost(state.posts, postId).user
return getUser(state.users, userId)
}

关于javascript - 使用选择器获取相关项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53775093/

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