-
Notifications
You must be signed in to change notification settings - Fork 48.3k
Add a React.Children.text
helper
#9255
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
Comments
Why not get On another note, you could easily write your own implementation |
I think we need to clarify how opaque |
I don't think it's likely. |
I needed something like this for a /**
* The `BaseButton` component accepts any number of children. This flexibility
* is used to support easily adding icons as children. However, we only want
* to include strings when building accessible labels. Otherwise, it would say
* [object Object] in the label.
*/
const getLabelFromChildren = (children) => {
let label = '';
React.Children.map(children, (child) => {
if (typeof child === 'string') {
label += child;
}
});
return label;
}; |
In case anyone else ends up here, I discovered this package today and so far it works great! https://github.com/fernandopasik/react-children-utilities import Children from 'react-children-utilities'
const MyComponent = ({ children }) => Children.onlyText(children)
|
Does the React project have a published criteria for what makes it into the library proper, and what should be handled by 3rd party libraries? This seems like something that'd be better in React, so if there was such criteria I'd love to grok it. |
my two cents: import React, { isValidElement } from 'react'
const hasChildren = element =>
isValidElement(element) && Boolean(element.props.children)
const ReactChildrenText = children => {
if (hasChildren(children)) return ReactChildrenText(children.props.children)
return children
} |
Right now I handling task where I receiving converted markdown to React elements and I want to get text for headline anchors. Such overkill without this suggested helper field/method. EDIT: this package solved my issue: https://github.com/lhansford/react-to-text |
I've created a component roughly like this, serving to wrap the behavior of the SVG
text
element to add more sophisticated handling of the string. You can imagine something similar for generating simplea
tags orspans
ordefn
or whatnot…The key point is that I've taken the
children
prop and assigned it totext
locally, which makes the assumption that it will be a string that I can split!However the React.Children docs discuss "dealing with the this.props.children opaque data structure" (emphasis mine). So technically my code's assumption could break in the future?
I would propose a
React.Children.text
helper that would guarantee stable behavior to this code:Specifically:
This would behave very much like the React.children.onlyChild logic and its invariant check, but expecting
typeof children === 'string'
rather than aReactElement.isValidElement(children)
. (See also #1104.)The text was updated successfully, but these errors were encountered: