Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning undefined from treeWalker throws error #66

Open
mattvague opened this issue May 20, 2021 · 2 comments
Open

Returning undefined from treeWalker throws error #66

mattvague opened this issue May 20, 2021 · 2 comments

Comments

@mattvague
Copy link

mattvague commented May 20, 2021

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!

@Lodin
Copy link
Owner

Lodin commented Jul 24, 2021

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
Copy link
Owner

Lodin commented Jul 24, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants