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

Use React.Children API for accessing props.children #478

Closed
Mossop opened this issue Apr 19, 2020 · 3 comments
Closed

Use React.Children API for accessing props.children #478

Mossop opened this issue Apr 19, 2020 · 3 comments

Comments

@Mossop
Copy link

Mossop commented Apr 19, 2020

In React props.children is intended to be an opaque type and for forwards compastibility you're meant to use the React.Children API for interacting with it (https://reactjs.org/docs/react-api.html#reactchildren). Right now though fluent uses it directly (f.e. https://github.com/projectfluent/fluent.js/blob/master/fluent-react/src/localized.ts#L53).

@stasm
Copy link
Contributor

stasm commented Apr 20, 2020

This code used to use React.Children.only, but in #230 we added support for wrapping <Localized> around strings only, rather than elements, and Children.only throws on a single string child, unfortunately.

// Localized wrapping an element with fallback
<Localized id="foo">
    <p>Fallback Foo</p>
</Localized>
// Localized wrapping a fallback string.
<Localized id="foo">
    Fallback Foo
</Localized>

In hindsight, I should have extracted the code path for string children into a separate component, e.g. <LocalizedText>. Perhaps that's something I could do in a future release (probably 2.x; I'd like to publish 1.x soon with the current API, even if it's not optimal).

Do you have other suggestions about how to fix this without inspecting props.children?

@Mossop
Copy link
Author

Mossop commented Apr 20, 2020

Ah, looks like I've always misunderstood the term "opaque" in the docs and it actually just means that you can't be sure what it is, not that it may be something unknowable: facebook/react#12913 (comment)

@Mossop Mossop closed this as completed Apr 20, 2020
@stasm
Copy link
Contributor

stasm commented Apr 20, 2020

Interesting, my first guess about opaque would be similar to yours. Thanks for finding that comment, it's helpful.

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

No branches or pull requests

2 participants