[React Intl] Use a react-intl Higher Order Component to format messages
React to use Component order format higher messages
2023-09-14 08:59:18 时间
In some cases, you might need to pass a string from your intl messages.js
file as a prop to a component. Instead of using react-intl
components (which generate markup), we’ll use the injectIntl
higher order component provided by react-intl
. This will provide just the string we’re looking for, make the prop simpler, and avoid creating unnecessary DOM elements.
We’ll also use props passed to the component from the Higher Order Component to clean up some redundant code.
import React from 'react'; import { injectIntl, FormattedMessage, FormattedHTMLMessage, FormattedRelative , FormattedTime, FormattedNumber } from 'react-intl'; import {meanBy, round, sortBy} from 'lodash'; import books from '../books.json'; const BookDetail = ({match, intl}) => { return ( <div className="BookDetail"> .... <textarea placeholder={intl.formatMessage({ id: 'detail.inputPlaceholder' })} cols="30" rows="10"></textarea> </div> ) } export default injectIntl(BookDetail);
相关文章
- react-router/v5之Router、Route、Redirect、Switch源码解析
- 【taro react】---- H5 通过 Jenkins 实现自动化部署
- React Hooks教程之基础篇
- React源码中的dom-diff
- react高频知识点梳理
- to program_I Just Want To
- react源码分析:babel如何解析jsx_2023-02-27
- 腾讯前端必会react面试题合集_2023-02-27
- 阿里前端二面react面试题_2023-02-28
- react源码解析14.手写hooks2
- react的jsx和React.createElement是什么关系?面试常问5
- React DOM Diff算法
- React-Hooks源码深度解读_2023-02-14
- ECC TO HANA FAGLB03 search-help on Account Number field doesn’t working or not returning the selected value to the Account Number field.详解编程语言