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

Components with refs getting "null" (string) key even if no key is set #1240

Closed
bdwain opened this issue Oct 8, 2017 · 6 comments · Fixed by #1254
Closed

Components with refs getting "null" (string) key even if no key is set #1240

bdwain opened this issue Oct 8, 2017 · 6 comments · Fixed by #1254

Comments

@bdwain
Copy link
Contributor

bdwain commented Oct 8, 2017

I have a component that returns something like

return <Dropdown ref={this.setRef}>...</Dropdown>;

I am trying to shallow render it, but when i try to verify the element returned from .get(0), it has a key of "null", the string not the value null, even though there should be no key in the return value.

I've been trying to dig through the code and I think the problem is here. The function propsWithKeysAndRef is adding BOTH key and ref to props, even if only one of them has a value. React stringifies all valid keys in createElement, and a key is valid if it is not undefined. This seems like it was an intentional decision.

The problem seems to be that in enzyme 3, it reruns the node through react.createElement, which now thinks you explicitly set the key to null, and so it sets it to "null". I guess an element with no key gets a "key" property of null, not undefined.

Unfortunately, I don't think the answer is to just not return key as one of the props from propsWithKeysAndRef if it is null, because if you explicitly set it to null, then that would be the correct thing to do.

Is there a way to store the original key value on the node somehow, so that you can look for undefined explicitly?

@bdwain bdwain changed the title Components with refs getting "null" key even if no key is set Components with refs getting "null" (string) key even if no key is set Oct 9, 2017
@bdwain
Copy link
Contributor Author

bdwain commented Oct 11, 2017

any update on this? I'd be happy to make a PR i just want to make sure I'm going in the right direction first.

@ljharb
Copy link
Member

ljharb commented Oct 11, 2017

I'm not sure what the solution is; but I think you've correctly identified the bug.

@ljharb
Copy link
Member

ljharb commented Oct 11, 2017

@bdwain what version of react are you using, and which adapter?

@bdwain
Copy link
Contributor Author

bdwain commented Oct 11, 2017

version 16.0.0, adapter 16@1.0.1

@bdwain
Copy link
Contributor Author

bdwain commented Oct 12, 2017

i can try to look at this later tonight.

@bdwain
Copy link
Contributor Author

bdwain commented Oct 12, 2017

added #1254 to fix this.

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

Successfully merging a pull request may close this issue.

3 participants