-
Notifications
You must be signed in to change notification settings - Fork 47k
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
[bug][16.5.0] option returns [object Object] instead of string #13586
Comments
|
@jquense |
If you remove |
we should make this work probably as well. IDK how to categorize this breakage, it worked solely because of how forgiving browsers can be (which is not unlimited causing the original bug), but the markup is definitely incorrect. It also definitely broke something that "worked" before |
Does not work with a context consumer inside a option either. |
I'm going to tag this as a regression for now, at least until we're sure one way or another. |
@quazzie You can fix that by wrapping |
@Simek thanks but in my real app i use a component that uses context internally so i can't really use that. |
@bvaughn yeah it seems to be connected to #13465 |
@quazzie Sure thing, it seems like it's a regression. For now if you want you can implement To be clear I'm just providing quick solutions until React team decided how to solve that issue. |
I don't think it was strictly a regression. This is kind of a thorny area. It was never intentionally supported. It accidentally worked on initial mount but then crashed on updates (#13261). Fixing the crash was more important, so we fixed it to be treated as text content (which it should be). Unfortunately this means putting custom components in the middle is not supported. That's consistent with how I think it's better to show invalid output and warn about something that breaks on updates, than to let people use it only to discover it crashes in production. But I can see arguments for why this should be supported when the custom component returns a string. Unfortunately I don't know how to fix it in a way that would both solve the update crashes and support text-only content. I think for now it's reasonable to say putting custom components into |
Since we warned about this before (in 15), and it crashes in unexpected ways, I think the new behavior is actually a bugfix — both for the missing warning, and for inconsistent behavior. It's very unfortunate that the warnings didn't get emitted in some cases (to be clear, it did for components returning DOM nodes — but we missed the case for strings). But I think allowing crashes in this case is worse than addressing the problem head on. The intended workaround if you're using react-intl is indeed this: https://codesandbox.io/s/6yr4xy5ll3 If you use something else, just make what you're passing to Both of these cases don't crash on updates, and work correctly. Again, very sorry we only discovered this now. |
@gaearon a single |
@nicolas-cherel You're welcome to file a proposal for how it should work (as long as it doesn't include crashes). |
To be clear — if you figure out how to make it work in all cases, I think we'd be happy supporting using custom components returning strings for this. I couldn't figure it out yet. |
The fact that we use these labels inside `<select/>` options prevent us from implementing this as a React component as for now React does not support having components inside `<option/>` tags, even if the component returns only strings. [This message](facebook/react#13586 (comment)) explains why its not supported (though it has been in the past) and why it may not be in a near future.
The fact that we use these labels inside `<select/>` options prevent us from implementing this as a React component as for now React does not support having components inside `<option/>` tags, even if the component returns only strings. [This message](facebook/react#13586 (comment)) explains why its not supported (though it has been in the past) and why it may not be in a near future.
Shouldn't this be kept open (or is there another issue tracking this?) even if nobody has any implementation ideas yet? It's totally reasonable to expect |
As a feature request, sure. Please file a new issue or search if one exists? |
@gaearon I see how this would be difficult to fix, but I'd argue that it still counts as a bug. Isn't it reasonable for a user of React to expect |
I am sorry to go a bit off track here, but what is the status for this lost warning? Is it something which won't be re-introduced? We had the exact case where we were using a translation component returning a string. The end result is that |
Just opened a feature request here #15513 |
@gaearon Maybe I'm missing something, but 16.4 did not warn about this code: import React from "react";
import ReactDOM from "react-dom";
const App = () => {
return (
<select>
<option>
<Foo />
</option>
</select>
);
};
function Foo() {
return "foo";
}
ReactDOM.render(<App />, document.getElementById("root")); This code worked just fine in React 16.4, but later broke in 16.5. In some of the code examples I've seen from 16.4.2: https://codesandbox.io/s/gifted-beaver-xjv1j Could we perhaps reevaluate this as a regression that needs to be fixed rather than an enhancement? I would expect that |
If anyone still needs an answer to the original question, as mentioned in earlier comments and in the documentation here, you cannot use a formatted message in the format of
instead I've found formating it as such does work:
where |
So any valid workaround that doesn't involves changing your entire component tree? Many people is using context inside options to provide localisation etc. |
Is there any solution to this till date? |
@hrupesh There's still no solution that isn't hacky. What I did and what my personal recommendation would be is to use a replacement over native select element, carefully crafted to keep accessibility levels high, like Reach UI Listbox. |
This is fixed in 18 Alpha. |
React 18 is released with the fix. |
What is the current behavior?
<option>
returns [object Object] instead stringDemo: https://codesandbox.io/s/ww5mv2w957
What is the expected behavior?
Expected string
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
The text was updated successfully, but these errors were encountered: