如何从本地图中获取数据?
我有显示分层数据的组件.它是一个递归数据结构——一个分层分类器.分类器中的每个类别由子类别等组成.
I've got the component which displays the hierarchical data. It's a recursive data structure - a hierarchical classifier. Each category in the classifier consists of child categories and so on.
这里是它的游乐场:
我有一个组件以第三方 html 形式显示这个分类器.它用于选择一个类别.
I've got a component showing this classifier in a third party html form. It is used to pick a category.
在某个时间点,用户将从这个递归分类器中选择一个类别,我将不得不获取导致当前级别的数据的完整路径.我的意思是,如果用户在层次结构的第 3 级选择一个类别,我必须返回这些数据:
At some point in time a user will pick a category from this recursive classifier and I will have to get the full path of data that led to the current level. I mean if a user picks a category at the level 3 of the hierarchy, I must give back these data:
[root cat id, root cat name],
[level 1 cat id, level 1 cat name],
[level 2 cat id, level 2 cat name]
那么,当我在第 2 级捕获组件内部采摘事件时,如何从 Relay
存储(从本地图)获取根类别的完整路径?
So when I catch the event of picking inside the component at the level 2 how can I get the full path from the root category from Relay
store (from local graph)?
这有一个 React 解决方案而不是 Relay 解决方案:如何访问树中给定节点的路径?我会合成一个路径,并将其传递到您的组件中.
This has a React solution rather than a Relay one: how to access the path to a given node in the tree? I would synthesize a path, and pass it into your component.
const path = [
// The elements of the passed-in path.
...this.props.path,
// A new path element using the details from this level.
[this.props.catId, this.props.catName],
];
<MyNode path={path} onClick={() => alert(path)} />
点击这里查看工作示例.