-
Notifications
You must be signed in to change notification settings - Fork 47.1k
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
Allow ReactNode as a type for the child of <option/> #15513
Comments
It would be good if you could summarise that previous thread, with examples of current behaviour, expected behaviour, and such. |
current behaviour: expected behaviour: I'm afraid same issue, |
it seems @kambleaa007's example didn't compile, but I fixed it: https://codesandbox.io/s/mw9y04j5j?fontsize=14 |
I think the solution you provided solves the issue. I think this can be closed. |
I am still not satisfied with that approach. The issue (or feature request) is for The sandbox shows that a ReactNode returns "[object Object]" instead of rendering the the child as a string. The fact that Plus it is just much simpler to drop a component in rather than going through the route that is shown. |
This has always been an issue. React < 0.14 used to silently accept invalid DOM structure, in your case elements inside elements. The browser would then correct the DOM structure, and cause the virtual DOM managed by React to be out of sync with the real thing. You wouldn't see errors until you tried to re-render existing components instead of just re-mounting them. react-intl V2.0.0, which will ship with support for React 0.14, allows you to use the Function-As-Child pattern to customize the way your Formatted* components render. See the "Function-As-Child Support" paragraph on this issue. |
ReactNode can either be Just because react-intl provides a workaround doesn't mean the inconsistency of |
If we think about it this way, tag has predictable usage and usually has string (text) as a child, anything more than that will be bogus for the DOM to correctly interpret and it defaults everything to a string. You should probably not use a select and option element if you want anything more than a string as a child of option. You can combine other custom (your own defined) react elements and react component to achieve what you want to achieve with the default behavior of the combination of select and option react element. |
@engprodigy I'm not sure you understand the request. The request is not to allow React components are not always DOM elements. This is a valid React component: function Hello() {
return "Hello World"
} When you use I think the technical challenge mentioned is if you do
This is more for consistency sake. We have something along the lines of: <ul>
<li>
<label>
<input type="radio" value={0} />
Option 1 (<FormatCurrency value="20" />)
</label>
</li>
<li>
<label>
<input type="radio" value={1} />
Option 2 (<FormatCurrency value="40" />)
</label>
</li>
</ul> And with changing from radios to a dropdown, we would expect to be able to do this: <select>
<option value={0}>
Option 1 (<FormatCurrency value="20" />)
</option>
<option value={1}>
Option 2 (<FormatCurrency value="40" />)
</option>
</select> But this fails. The <select>
<option value={0}>
Option 1 ({formatCurrency(20, this.context.currency)})
</option>
<option value={1}>
Option 2 ({formatCurrency(40, this.context.currency)})
</option>
</select> We do not wish to expose context to the component containing the options. The reason we want this fixed is because it creates a lot of foot-gun problems with our engineers. A few of us (from different teams) have experienced this issue on our own and eventually came across the bug listed at the head of this feature request. It is not obvious that it "does not work as expected". It simply changes the text to |
It's amazing this is not considered a regression: React 16.4.2 works: https://codesandbox.io/s/gifted-beaver-xjv1j Same code in both instances: 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")); |
Great Catch |
@lukescott I will agree if all the components and elements inserted in an option tag are expected to return a string only, then it should work. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution. |
Yeh, Thanks |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any additional information, please include with in your comment! |
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you! |
This is fixed in 18 Alpha. |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
Currently, the options element only allows types number and string.
What is the expected behavior?
An option should allow for a ReactNode as a child in addition to a number + string.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
All versions.
All browser types.
To the best of my knowledge, no.
p.s. This is my first feature request here, so let me know if I need to adjust the feature request in any way.
The text was updated successfully, but these errors were encountered: