React-Intl How to use FormattedMessage in input placeholder

前端 未结 11 645
走了就别回头了
走了就别回头了 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:02

    Based on the react intl wiki the implementation of an input box with translatable placeholder will look like:

    import React from 'react';
    import { injectIntl, intlShape, defineMessages } from 'react-intl';
    
    const messages = defineMessages({
      placeholder: {
        id: 'myPlaceholderText',
        defaultMessage: '{text} and static text',
      },
    });
    
    const ComponentWithInput = ({ intl, placeholderText }) => {
      return (
        
      );
    };
    
    ComponentWithInput.propTypes = {
      intl: intlShape.isRequired
    };
    
    export default injectIntl(ComponentWithInput);
    

    and the useage of it:

    import ComponentWithInput from './component-with-input';
    ...
    render() {
      
    }
    ...
    

    The id: 'myPlaceholderText', part is necessary to enable the babel-plugin-react-intl to collect the messages for translation.

提交回复
热议问题