2015-10-30 9 views
8

私はアプリケーションをReact 0.12からReact 0.14に移行しようとしており、react-intl FormattedMessageオブジェクトを選択タグ内に配置したオプション要素に問題があります。 0.12を反応させ、私は翻訳されたオプションの要素を参照してくださいにReact 0.14のオプションタグ内でFormattedMessageを使用するには?

<select> 
<option value="value1"><FormattedMessage message={this.getIntlMessage('key1')}/></option> 
<option value="value2"><FormattedMessage message={this.getIntlMessage('key2')}/></option> 
</select> 

このコードは正常に動作します:ここで

はサンプルJSXコードです。

Only strings and numbers are supported as <option> children. 

私は今年の初めに起こった反応という点で、このチェンジにメッセージをトレースさ:

https://github.com/facebook/react/pull/3847/files

私はこの問題をどのように修正することができます

では、私はこのエラーを得た、0.14の反応します?国際化されたオプション要素を使用しようとする唯一の人にはなりませんか?

答えて

12

これは常に問題となっています。あなたのケースでは<span>要素が<option>要素内にある無効なDOM構造を自動的に受け入れるために、< 0.14が使用されました。ブラウザはDOM構造を修正し、Reactによって管理される仮想DOMを本物のものと同期させないようにします。既存のコンポーネントを再マウントするのではなく、既存のコンポーネントを再レンダリングしようとするまで、エラーは表示されません。 0.14を反応させるのためのサポートが付属します

react-intl V2.0.0は、あなたがあなたのFormatted*コンポーネントがレンダリング方法をカスタマイズする機能-AS-子パターンを使用することができます。 this issueの「機能別子サポート」の段落を参照してください。あなたのケースでは

、あなたはどうなる:

<FormattedMessage message={this.getIntlMessage('key1')}> 
    {(message) => <option value="value1">{message}</option>} 
</FormattedMessage> 
<FormattedMessage message={this.getIntlMessage('key2')}> 
    {(message) => <option value="value2">{message}</option>} 
</FormattedMessage> 

私は、現在の安定版、1.2.1でこれを達成する方法はないと思います。

+0

OPがフォーマットを行う必要がない場合は、オプションの中で 'this.getIntl​​Message( 'key1')'だけを実行できますか?完全な根本的な翻訳方法を公開しないような見落としがあります。 –

+1

OPのケースでは、this.getIntl​​Message( 'key1') 'は正しいメッセージを返します。 V2.0。0 APIは、コンテキスト上で翻訳メソッドを公開します。 –

+3

プレースホルダを使用しない限り動作するようですが、これを行う安定した方法がないとは思いませんか? –

2

@Alexandre Kirszenbergの答えに少しより良い代替として、それはコンポーネントにintlオブジェクトを注入し、直接formatMessage機能を使用することも可能ですが、

import { injectIntl, intlShape, defineMessages, FormattedMessage } from 'react-intl'; 

const AddressForm = ({ intl, street, number, postalCode, city, onChange }) => { 
    return (
    <form id="paymentAddress"> 
     // ... 
     <fieldset className="form-group"> 
     <label htmlFor="country"><FormattedMessage { ...messages.country } />:</label> 
     <div> 
      <select name="country"> 
      <option value="DE">{intl.formatMessage(messages.de)}</option> 
      <option value="UK">{intl.formatMessage(messages.uk)}</option> 
      <option value="CH">{intl.formatMessage(messages.ch)}</option> 
      </select> 
     </div> 
     </fieldset> 
    </form> 
); 
}; 

AddressForm.propTypes = { 
    intl: intlShape.isRequired, 
    // ... 
} 
10

私は(同じ問題を抱えていたし、injectIntl​​を経由して、それを解決しました)。助け

import React, {Component, PropTypes} from 'react'; 
import {defineMessages, injectIntl, intlShape} from 'react-intl'; 

const messages = defineMessages({ 
    firstoption: { 
     id: 'mycomponent.firstoption', 
     defaultMessage: 'Coffee', 
    }, 
    secondoption: { 
     id: 'mycomponent.secondoption', 
     defaultMessage: 'Tea', 
    } 
}); 

class MyComponent extends Component { 
    render() { 
     const {formatMessage} = this.props.intl; 

     return (
      <div> 
      <select> 
       <option value="value1">{formatMessage(messages.firstoption)}</option> 
       <option value="value2">{formatMessage(messages.secondoption)}</option> 
      </select> 
      </div> 
     ); 
    } 
} 

MyComponent = { 
    intl : intlShape.isRequired 
}; 

export default injectIntl(MyComponent) 

希望...

+0

複数のコンポーネントで書式設定を使用する必要がある場合はどうでしょうか。どこで私はローカルにintl propを注入できますか?ユーザーがメッセージの書式設定を呼び出すときに毎回intlを注入する必要はありません – user1234

+1

コンポーネントを使用するときに、必要なintl属性に何を渡しますか?私はこれがインジェクションによって処理されたと仮定しましたが、必要な属性intlが存在しないことを示すコンポーネントが使用されるエディタでエラーが発生します。それはちょうど編集者の問題ですか? –

0

使用injectIntlはあなたにコンポーネントをラップする:あなたがしなければならないすべてはそのように、injectIntl​​機能を使用してコンポーネントをラップであることを意味し

This function is used to wrap a component and will inject the intl context object created by the IntlProvider as a prop on the wrapped component. Using the HOC factory function alleviates the need for context to be a part of the public API.

内部でAPIを使用したいので、formattedMessageなどのAPIを使用できます。 react-intl/wiki/API

関連する問題