react-vtree icon indicating copy to clipboard operation
react-vtree copied to clipboard

Returning undefined from treeWalker throws error

Open mattvague opened this issue 4 years ago • 2 comments

Hey there, looks like currently Tree.tsx assumes that treeWalker will always yield valid node data and that it will never be undefined, even though that is a valid return value for yield expressions (there is even a non-null assertion on the yielded value).

I'd like to propose that this behaviour be changed so that undefined values are handled gracefully as it would allow returning early in the case of not having nodes to render yet, etc, whereas right now an Cannot read property 'data' of undefined error is thrown.

@Lodin I'd be happy to PR a fix here but I'm not quite sure what the desired behaviour would be in this case, should I just return early with null inside of generateNewTree?

Anyways, thanks for an otherwise awesome library!

mattvague avatar May 20 '21 22:05 mattvague

Hi @mattvague. Sorry for making you wait for so long time.

Could you please provide an example of how you want to use yielding null? According to my design, you can just skip children yielding (e.g. if the children array is empty or the ), and everythig will work as expected.

Lodin avatar Jul 24 '21 21:07 Lodin

Oh, I misread a part of you message. About yielding undefined: it is not possible because by the undefined value the start of the new node is detected. You can see it in the example:

function* treeWalker() {
  // intro

  while (true) {
    const parent = yield;  // Here is the trick
  }
}

Here is the detection in the code.

Lodin avatar Jul 24 '21 21:07 Lodin