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

"Unable to find element" error on <option> elements #2620

Closed
mmavko opened this issue Nov 28, 2014 · 5 comments · Fixed by #3847
Closed

"Unable to find element" error on <option> elements #2620

mmavko opened this issue Nov 28, 2014 · 5 comments · Fixed by #3847

Comments

@mmavko
Copy link

mmavko commented Nov 28, 2014

If the contents of the <option> element is constructed in special way, it breaks React on component update with "Unable to find element" error. Here's an example of render method body:

    var v1 = String(Math.random());
    var v2 = String(Math.random());
    return <select><option value={v2}>{v1} ({v2})</option></select>;

Here's working jsfiddle (with React 0.12.0): http://jsfiddle.net/rp3fz0nf/1/

Adding key={v2} to option props fixes the issue.

@waldreiter
Copy link
Contributor

React separates the <option>'s children with <span>s. In HTML only text is allowed there and the browser handles it gracefully by removing the <span>s. On the next render React tries to change those <span>s, but can't find them.

A workaround is to give a string to the <option>:

var text = v1 + '(' + v2 + ')';
return <select><option value={v2}>{text}</option></select>;

@mmavko
Copy link
Author

mmavko commented Nov 28, 2014

Thanks for explaining this. In such case probably the best solution would be to extend error explanation with information about "option" elements (behind "table" etc).

@zpao
Copy link
Member

zpao commented Dec 10, 2014

Yea, we should probably do some checks in our ReactDOMOption wrapper and not allow anything but text as children.

@ryanflorence
Copy link
Contributor

Just hit this http://jsfiddle.net/kg7g09cq/

@sophiebits
Copy link
Collaborator

This can be fixed by using ReactChildren.forEach in ReactDOMOption to flatten the children before passing it to the raw HTML component. Strings and numbers get concatted together; other elements should be skipped and log a warning for invalid children.

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.

5 participants