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

Consider Exposing React Components as an Alternate API #23

Closed
sebmarkbage opened this issue Oct 14, 2014 · 3 comments · Fixed by #24
Closed

Consider Exposing React Components as an Alternate API #23

sebmarkbage opened this issue Oct 14, 2014 · 3 comments · Fixed by #24

Comments

@sebmarkbage
Copy link

It would be cool to have format.js React components instead of the mixin. Something like:

return <div><Number style="percent">{0.9}</Number></div>

That way you don't need the mixin in your component.

Currently this requires an extra wrapper element, but I think we'll be able to get rid of that limitation in React.

@juandopazo
Copy link
Contributor

@ericf just had this same idea last week!

@ericf
Copy link
Collaborator

ericf commented Oct 14, 2014

Yeah, I had this same thought last week. The declarative form reads much better (when using JSX).

In the example you have above, you'd need to also pass a value for the locales prop. In general we found that threading the locales, formats, and messages props through the entire component hierarchy gets noisy. Which is why we ended up going the mixin route to begin with.

So I think we'll still want this package to also export a mixin that can optionally be used to pipe the i18n metadata through the hierarchy of components via the context. This way you could do something like:

<App locales="en" messages={...} formats={...}>
  <Number style="percent">{0.9}</Number>
  <div>
    <Number>{1000}</Number>
  </div>
</App>

The App component would use the mixin (as would Number, Date, etc.) so that Number could then reference the locales value within its context. This would keep things DRY since these i18n metadata values are usually constant through the app's rendering hierarchy.

@sebmarkbage do you think that combo sounds like a good design (mixin being optional, but helps keep things DRY)?

@sebmarkbage
Copy link
Author

Yea, you can use context to implicitly thread the locales down.

The app just needs to do something like:

getChildContext() {
  return: { reactIntlLocale: 'en' };
}

ericf added a commit to ericf/react-intl that referenced this issue Oct 16, 2014
This adds React components for each format type, e.g. `Number`, `Date`,
`Message`, et al. These components have been added in a way that's
backwards compatible with v1.0.0.

The new way to use this package is to reference the new `ReactIntl`
global in the browser, or use `var ReactIntl = require('react-intl');`
in Node.js.

Instead of the Mixin being the default export, the `ReactIntl` module
now contains a named export for each component, the mixin, and the
function to add locale data. Completely they are:

```
// Component Mixin:
ReactIntl.Mixin

// Components: (e.g., <Number style="percent">{0.9}</Number>)
ReactIntl.Date
ReactIntl.Time
ReactIntl.Relative
ReactIntl.Number
ReactIntl.Message

// Function to add locale data:
ReactIntl.__addLocaleData
```

**Note:** This should be safe to release as `v1.1.0` as all back-compat
is maintained.

Closes formatjs#23
@ericf ericf closed this as completed in #24 Dec 10, 2014
longlho pushed a commit that referenced this issue Apr 27, 2020
Make Browserify bundle work in ES3 environments
wevdev85 pushed a commit to wevdev85/JavaScriptDoc-baseline that referenced this issue Aug 27, 2021
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

Successfully merging a pull request may close this issue.

3 participants