Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 重新选择关系数据的选择器_Reactjs_Redux_Reselect - Fatal编程技术网

Reactjs 重新选择关系数据的选择器

Reactjs 重新选择关系数据的选择器,reactjs,redux,reselect,Reactjs,Redux,Reselect,选择器有两个参数,状态和道具,但是我们如何处理关系数据的选择器呢 initialState = { department :{ids:[1],byId:{name:'dep 1',id:1,parent:null}} sections :{ids:[2],byId:{name:'section 1.1',id:2,parent:1}} subsections :{ids:[3],byId:{name:'subsection 1.1.1',id:3,parent:2}} }

选择器有两个参数,状态和道具,但是我们如何处理关系数据的选择器呢

initialState = {
  department  :{ids:[1],byId:{name:'dep 1',id:1,parent:null}}
  sections    :{ids:[2],byId:{name:'section 1.1',id:2,parent:1}}
  subsections :{ids:[3],byId:{name:'subsection 1.1.1',id:3,parent:2}}
}
这里的部门是n(部门)的祖父母和n(子部门)的祖父母

中,我想选择部门1及其所有子部门

如何编写这样的选择器而不将我需要的部门id传递给选择器?

您可以使用路由器(react router)通过url段传递这些密钥,然后您可以访问所需的id,即:

mapStateToProps(state, ownProps) {
  // then get access to id here
  console.log(ownProps.params.department_id);
}

在这里,您将发现一个实现,我希望它将回答您的第一个问题:“我们如何处理关系数据的选择器?”

从“重新选择”导入{createSelector};
常量状态={
部门:[{id:1,byId:{name:“dep1”,id:1,parent:null}],
节:[{id:2,byId:{name:“section 1.1”,id:2,parent:1}}],
小节:[
{
id:3,
byId:{name:“第1.1.1小节”,id:3,父项:2}
}
]
};
const clone=obj=>JSON.parse(JSON.stringify(obj));
常量自底向上=(…级别)=>
级别。减少((儿童、父母)=>{
const p=克隆(父母);
const addToParent=child=>{
const父=p查找(PAL= > PARID==儿童.ByID.Primor);
如果(家长){
if(父项、子项){
父。子。推(子);
}否则{
parent.children=[child];
}
}
}
儿童。forEach(addToParent);
返回p;
});
const selectSubs=state=>state.subsections;
const selectSecs=state=>state.sections;
const selectDeps=state=>state.departments;
const selectHierarchy=createSelector(
选择Subs,
选择秒,
选择deps,
自下而上
);
log(选择层次结构(状态));
此新选择器将返回:

如您所见,由于我没有完全理解您的存储结构,所以我对其进行了一些更改:每个域都变成了一个数组,每个id都变成了一个数字

我希望这会有帮助。。。 干杯