gpt4 book ai didi

reactjs - 规范化状态的 Redux 选择器

转载 作者:行者123 更新时间:2023-12-05 06:28:23 26 4
gpt4 key购买 nike

我需要帮助为基于来自 redux docs 的文档规范化的状态创建选择器

状态

{
"entities": {
"people": {
"byId": {
"1": {
"name": "Test User 1",
"classes": ["class1", "class2"],
"isSelected": true
},
"2": {
"name": "Test User 2",
"classes": ["class2", "class3"],
"isSelected": false
},
"3": {
"name": "Test User 3",
"classes": ["class4"],
"isSelected": false
}
},
"allIds": ["1", "2", "3"]
},
"classes": {
"byId": {
"class1": {
"name": "Class 1",
"courses": ["course1", "course2"]
},
"class2": {
"name": "Class 2",
"courses": ["course2"]
},
"class3": {
"name": "Class 3",
"courses": ["course1", "course3"]
}
},
"allIds": ["class1", "class2", "class3"]
},
"courses": {
"byId": {
"course1": {
"name": "Course 1"
},
"course2": {
"name": "Course 2"
},
"course3": {
"name": "Course 3"
}
},
"allIds": ["course1", "course2", "course3"]
}
}
}

在每个人的个人资料上,我想显示相关的类(class)和类(class)。我无法创建选择器来访问此信息。

人物选择器

import { createSelector } from 'reselect';

const getPeopleDetails = createSelector(
state => state.entities.people,
(people) => people.allIds.reduce(
(acc, cV) => {
if(people.byId[cV].isSelected) acc.push(people.byId[cV]); return acc;
}, [])
);

export default {
getPeopleDetails
}

因为 createSelector 不接受任何参数。对于我的 courses 状态切片;如何创建一个选择器,从中获取当前选定人员个人资料的信息?

我应该创建一个联结表来描述实体之间的关系吗?

(我对 react/redux 很陌生)

最佳答案

你的选择器可以接受 props 和 state。然后你可以将它用作选择器的参数:

const getPerson = (state, props) =>
state.entities.people.byId[props.id];

const getPersonDetails = createSelector(
getPerson,
state => state.entities.classes,
(person, classes) => {
// loop through classes, etc..
// return whatever
}
);

关于reactjs - 规范化状态的 Redux 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54375404/

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