React-sortable-tree: No node found at the given path

Created on 6 Nov 2017  Β·  4Comments  Β·  Source: frontend-collective/react-sortable-tree

All packages are up to date,This is an example of how to reproduce this error

https://codesandbox.io/s/ywlkpqnmnj

Step 1: Click tmp option to expand.

Step 2: Click a child of tmp.

Step 3: Click src option to expand.

Step 4: Click a child of src .

In my project, I need get the last key path to find the last node data.
Please click on the options in order

Most helpful comment

@fritz-c Thank you very much for your help, you solved my problem

All 4 comments

Thank you for reproducing the issue with codesandbox. That really helped.
The issue is that the path changes whenever you expand the src node, because by default, all nodes use their position in the visible part of the tree as their key
i.e.,

folder 0/         // treeIndex === key === 0
β”œβ”€ [─] folder 1/  // treeIndex === key === 1
β”‚Β Β  β”œβ”€β”€ file1.js  // treeIndex === key === 2
β”‚Β Β  └── file2.js  // treeIndex === key === 3
β”œβ”€ [+] folder 2/  // treeIndex === key === 4
β”œβ”€ [─] folder 3/  // treeIndex === key === 5
β”‚Β Β  └── file1.js  // treeIndex === key === 6

So if I opened up folder 2 here, and it had one child, the keys would change as follows:

folder 0/         // treeIndex === key === 0
β”œβ”€ [─] folder 1/  // treeIndex === key === 1
β”‚Β Β  β”œβ”€β”€ file1.js  // treeIndex === key === 2
β”‚Β Β  └── file2.js  // treeIndex === key === 3
β”œβ”€ [-] folder 2/  // treeIndex === key === 4
β”‚Β Β  └── file1.js  // treeIndex === key === 5
β”œβ”€ [─] folder 3/  // treeIndex === key === 6
β”‚Β Β  └── file1.js  // treeIndex === key === 7

So if I had a path of [5, 6] from the first state of the tree, in the second state of the tree, it would look on the top level for a node with a key of 5, which we can see has been reassigned to a file on another level down, and will fail.

So you will need to come up with some unique identifier for your nodes (or add them arbitrarily before putting them in the component), and return that from getNodeKey (e.g., getNodeKey={({ node }) => node.id}).
I use treeIndex as described above to simplify configuration in basic use cases, but setting a getNodeKey to suit your own data is the best way to get stuff like this to work reliably.

@fritz-c Is it better using absulote position as treeIndex, seems could solve this problem.

folder 0/         // treeIndex === 0
β”œβ”€ [─] folder 1/  // treeIndex === 0.0
β”‚   β”œβ”€β”€ file1.js  // treeIndex === 0.1
β”‚   └── file2.js  // treeIndex === 0.2
β”œβ”€ [-] folder 2/  // treeIndex === 1
β”‚   └── file1.js  // treeIndex === 1.0
β”œβ”€ [─] folder 3/  // treeIndex === 2
β”‚   └── file1.js  // treeIndex === 2.0

@fritz-c Thank you very much for your help, you solved my problem

@hapood That might solve the treeIndex problem. I'll have to look into that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xmhscratch picture xmhscratch  Β·  5Comments

jorgecuesta picture jorgecuesta  Β·  4Comments

JonatanGarciaClavo picture JonatanGarciaClavo  Β·  3Comments

Suremotoo picture Suremotoo  Β·  4Comments

29er picture 29er  Β·  5Comments