问题
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.
Here's the playground for it:
Relay playground
I've got a component showing this classifier in a third party html form. It is used to pick a category.
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]
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)?
回答1:
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)} />
Click here for a working example.
来源:https://stackoverflow.com/questions/38339060/how-to-get-data-from-the-local-graph