Comments (9)
For these components we ideally want them to just emit text on render and leave it up to the developer to wrap them how they see fit. The React core team has talked about possibly supporting this idea that components can simply render text, and I think that feature would be a good fit for React Intl.
If we switch things to render a <time>
node, things get a bit tricky since we'd then need to pass along all the props to that node for things like className
, style
, etc. If we were to do this same for <FormattedNumber>
these props would clash since style
is used as an option to number formatting.
I think we should continue the discussion on this to figure out what the best thing to do is, and whether or not support for rendering text nodes is still in the cards for React.
Right now to do what you want, you can easily wrap it:
<time dateTime={this.props.date}>
<FormattedRelative value={this.props.date} />
</time>
from formatjs.
This is also related to the discussion in #72
from formatjs.
Thanks for the considered response @ericf.
I agree that if React can emit only text then it should be up to the developer to wrap that response as required. If not then the transfer of props is an interesting issue to be considered.
from formatjs.
@ericf in the docs I see
_formatDate(), formatTime(), formatRelative(), formatNumber(), and formatMessage() methods provide an imperative API to formatting data and strings, but these methods will likely be deprecated in v2.0, so use the Components._
so how should I implement this then?
<time dateTime={this.props.date} title={this.humanReadableDate()} >
<FormattedRelative value={this.props.date}/>
</time>
from formatjs.
@onemanstartup I don't follow what you're asking; you're example is using the React Intl Components.
from formatjs.
@ericf sorry, I mean rendering time in title. Without component.
from formatjs.
We're exploring a couple options for this:
- Keep the methods on the mixin. This has the problem of React moving away from using or encouraging the use of mixins.
- Create a FormatJS lib. Right now we have the core
Intl*
APIs that are built in to JavaScript, plus the ones we created for message and relative time formatting, but we could create a higher-level imperative API. This would allow us to add the caching and intl metadata features at a lower level than our template and component integration libs like React Intl.
We've been exploring option 2, but have some reservations around creating a this higher-level imperative library when we want to contribute back to the ECMAScript spec. But it may be the practicality of it that ends up bringing it to live. Right now we have 4 integration libraries that each have to do similar work to extract and build all the locale data, implement Intl*
instance memoization, and add connivence for managing intl metadata. We're feeling the pain of our current architecture and could move to something like:
To use FormatJS and React Intl there's two libraries you need to use: format.js
and react-intl.js
. The FormatJS lib would provide the imperative API, memoization, intl metadata handling, while React Intl would be the declarative component layer on top.
So for your example you would be able to access the FormatJS lib directly to implement this.humanReadableDate()
.
Whatever approach we end up going with we'll provide a way to handle what you're trying to do.
from formatjs.
@ericf thank you for explanation. Second version is seems good way to go.
from formatjs.
Closing this, we're keeping with the suggestion to wrap.
from formatjs.
Related Issues (20)
- @formatjs/intl-numberformat @ 8.9.0 -> 8.10.0 breaks Chrome 104 HOT 1
- cli-lib: onMsgExtracted never gets called HOT 1
- Update translation of 'last year' for Swedish locale HOT 1
- @formatjs/cli-lib is not an ESM module HOT 1
- formatMessage with ICU behave different then formatNumber HOT 2
- [@formatjs/intl Error MISSING_DATA] should be a warning not an error HOT 3
- [@formatjs/intl-durationformat] package.json deprecation warning HOT 2
- @formatjs/intl-messageformat, 10.5.0: Plural rules aren't applied correctly HOT 2
- @formatjs/intl-numberformat/polyfill: roundingIncrement seems to be a no-op HOT 3
- [docs] Rich Text Formatting example doesn't work HOT 2
- Polyfill.io has been sold to a weird Chinese company and should no longer be recommended HOT 1
- chore: update tzdata to 2024a
- INVALID_TAG when parsing regular HTML tag with attribute HOT 1
- Can't catch error HOT 1
- Feature request: allow units greater than `day` in `FormattedRelativeTime` with `updateIntervalInSeconds` to be picked HOT 2
- It's incredibly slow on lower end Android devices HOT 2
- Intl.Segmenter: isWordLike does not match Node.js and browsers HOT 2
- @formatjs/intl-locale/polyfill-force TypeError: Error uninitialized locale at Locale2.getWeekInfo HOT 3
- .formatMessage is not working when provided messages has object with `type: 1` HOT 8
- Extract does not work on vue template since vue 3.4.0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.