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

Document why <Formatted*> components are better than the imperative API #117

Closed
slorber opened this issue May 22, 2015 · 10 comments
Closed
Labels

Comments

@slorber
Copy link

slorber commented May 22, 2015

Hi,

Just wanted you to be aware that we had in Paris a ReactJS Meetup about I18n and I asked a question nobody could answer: what's the advantage of using React components for formatting instead of using directly the - maybe deprecated - mixin API this.format().

I think it should be clearly documented what are the advantages of a component over an imperative API

The only thing I guess so far is maybe the possibility to inject React components as template parameters, and maybe cacheability but I'm not sure at all :)

@ericf
Copy link
Collaborator

ericf commented May 28, 2015

This is a good idea and I can add docs about this. That said, there will be an imperative API because it's still needed for certain types of formatting; e.g., an element's prop value, like <input placeholder={someFormattedString} />.

A major area where the components provide value is in composition of complex messages See: https://github.com/yahoo/react-intl/blob/master/src/components/message.js

And as we gain more guidance from React on context and shouldComponentUpdate(), the components can abstract away whether or not the relatively expensive format() methods need to be called during a re-render.

@ericf ericf changed the title Document why <RelativeFormat> is better than this.format Document why <Formatted*> components are better than the imperative API May 28, 2015
@export-mike
Copy link

Yes I'm trying to pass in placeholder values, I'm using 2.0.0-beta-2, I've looked a the code and can't see a way to do this?

@slorber
Copy link
Author

slorber commented Feb 12, 2016

@export-mike

var placeholder = this.props.intl.formatMessage({ id: "searchBar.searchPlaceholder" });

@export-mike
Copy link

hmmm. shame I can't get this to be apart of all props in some way without having to decorate many components. thanks for your help @slorber

@slorber
Copy link
Author

slorber commented Feb 12, 2016

@export-mike be part of all props is like using react context, and decorator only extracts from the context.

What is your problem with the following I don't understand?

var React = require("react");
var ReactIntl = require("react-intl");

var SearchBar = React.createClass({
        ...
    render() {
        var placeholder = this.props.intl.formatMessage({ id: "searchBar.searchPlaceholder" });
        var inputValue = this.input();
        return (
                <input
                    value={inputValue}
                    onChange={this.onInputChange}
                    forceVisibility={this.isValid(inputValue)}
                    placeholder={placeholder}
                />
        );
    }
});

module.exports = ReactIntl.injectIntl(SearchBar);

@ericf
Copy link
Collaborator

ericf commented Feb 12, 2016

@export-mike are you talking about <input placeholder=""> or passing in values to fill placeholders inside of messages? For the former, what @slorber said, for the latter:

<FormattedMessage
  id="greeting"
  defaultMessage="Hello, {name}!"
  values={{
    name: 'Eric'
  }}
/>

@export-mike
Copy link

It's to populate a property that expects a string such as placeholder. I'm
now using injectIntl on my components but being more aware/cautious on what
components should be coupled to InjectIntl.

On Fri, 12 Feb 2016 19:50 Eric Ferraiuolo [email protected] wrote:

@export-mike https://github.com/export-mike are you talking about or passing in values to fill placeholders inside of
messages? For the former, what @slorber https://github.com/slorber
said, for the latter:

<FormattedMessage
id="greeting"
defaultMessage="Hello, {name}!"
values={{
name: 'Eric'
}}/>


Reply to this email directly or view it on GitHub
#117 (comment).

@ericf
Copy link
Collaborator

ericf commented Mar 7, 2016

@export-mike Here are some docs on the injection API: https://github.com/yahoo/react-intl/wiki/API#injection-api

@ericf ericf removed the enhancement label Mar 8, 2016
@ericf
Copy link
Collaborator

ericf commented Apr 5, 2016

Put some details on "Why Component?" on the Wiki:
https://github.com/yahoo/react-intl/wiki/Components#why-components

@ericf ericf closed this as completed Apr 5, 2016
@export-mike
Copy link

@ericf thanks for your help.

longlho pushed a commit that referenced this issue Apr 27, 2020
…, commas and element types… (#120)

* Print white-spaces and commas correctly from AST

Fixes #117

* Print element type correctly from AST

Correctly prints `number`, `date` and `time` element types in string format, instead of numbers `2`, `3` and `4`, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants