我需要帮助为基于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"]
}
}
}
在每个人的个人资料上,我想显示相关的班级和课程。我无法创建选择器来访问此信息。
人民的选择器
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非常陌生)
答案 0 :(得分:0)
您的选择器可以同时显示道具和状态。然后,您可以将其用作选择器的参数:
import_role
答案 1 :(得分:0)