React-Intl How to use FormattedMessage in input placeholder

前端 未结 11 606
走了就别回头了
走了就别回头了 2020-12-13 03:20

I\'m unsure how to get the values from


into a placeholder format like input:<

相关标签:
11条回答
  • 2020-12-13 04:13

    Consider this possibility.

    The simplest solution

    <IntlMessages id="category.name">
        {text => (
            <Input placeholder={text} />
        )}
    </IntlMessages>
    

    OR

    0 讨论(0)
  • The <Formatted... /> React components in react-intl are meant to be used in rendering scenarios and are not meant to be used in placeholders, alternate text, etc. They render HTML, not plain text, which is not useful in your scenario.

    Instead, react-intl provides a lower level API for exactly this same reason. The rendering components themselves use this API under the hoods to format the values into HTML. Your scenario probably requires you to use the lower level formatMessage(...) API.

    You should inject the intl object into your component by using the injectIntl HOC and then just format the message through the API.

    Example:

    import React from 'react';
    import { injectIntl, intlShape } from 'react-intl';
    
    const ChildComponent = ({ intl }) => {
      const placeholder = intl.formatMessage({id: 'messageId'});
      return(
         <input placeholder={placeholder} />
      );
    }
    
    ChildComponent.propTypes = {
      intl: intlShape.isRequired
    }
    
    export default injectIntl(ChildComponent);
    

    Please note that I'm using some ES6 features here, so adapt according to your setup.

    0 讨论(0)
  • 2020-12-13 04:16

    In my case I had the whole app in one file, so using export wouldn't work. This one uses the normal class structure so you can use the state and other functionality of React if needed.

    class nameInputOrig extends React.Component {
      render () {
        const {formatMessage} = this.props.intl;
        return (
            <input type="text" placeholder={formatMessage({id:"placeholderIntlText"})} />
        );
      }
    }
    
    const nameInput = injectIntl(nameInputOrig);
    

    Apply using the created constant:

    class App extends React.Component {
        render () {
            <nameInput />
        }
    }
    
    0 讨论(0)
  • 2020-12-13 04:19

    As from React version >= 16.8, you can use useIntl hook :

    import React from 'react';
    import { IntlProvider, useIntl } from 'react-intl';
    
    const FunctionComponent = () => {
        const intl = useIntl();
        const lang = "en";
        const messages = {
          en: {
            'placeholderMessageId': 'placeholder in english',
          },
          fr: {
            'placeholderMessageId': 'placeholder en fançais',
          }
        }
        return ( 
          <IntlProvider locale = {lang} messages = { messages[lang] } >
              <input placeholder = { intl.formatMessage({ id: 'placeholderMessageId' })}/> 
          </IntlProvider >
          );
        };
    
    export default FunctionComponent;
    
    0 讨论(0)
  • 2020-12-13 04:21

    Like this:

    import React, {PropTypes} from 'react';
    import { injectIntl, FormattedMessage } from 'react-intl';
     
    /**
    * {
    * "hello": "Hello",
    * "world": "World"
    * }
    */
     
    // pure function
    const PureFunciton = injectIntl(({ intl }) => {
    return (
      <div>
        <p>{intl.formatMessage({ id: 'hello' })}</p>
        <p><FormattedMessage id="world" /></p>
      </div>
    )
    });
     
    // class Component
    class componentName extends Component {
      handleStr = () => {
        // return 'Hello';
        const { intl } = this.props;
        return intl.formatMessage({ id: 'hello' })
      }
      render() {
        return (
          <div>
            <p>{this.handleStr()}</p>
            <p><FormattedMessage id="world" /></p>
          </div>
        );
      }
    }
     
    export default injectIntl(connect(componentName));
    
    0 讨论(0)
提交回复
热议问题