Created
December 20, 2018 10:02
-
-
Save PsixokoT/87d626e628f2be6d3b910bd165eb2d2b to your computer and use it in GitHub Desktop.
How convert to ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface InjectedIntlProps { | |
intl: InjectedIntl; | |
} | |
interface InjectIntlConfig { | |
intlPropName?: string; | |
withRef?: boolean; | |
} | |
function injectIntl<P>(component: React.ComponentType<P & InjectedIntlProps>, options?: InjectIntlConfig): | |
React.ComponentClass<Pick<P, Exclude<keyof P, keyof InjectedIntlProps>>> & { WrappedComponent: React.ComponentType<P & InjectedIntlProps> }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
import { injectIntl } from 'react-intl'; | |
export function withTranslate(Component) { | |
function WithTranslate(props) { | |
const { intl, ...compProps } = props; | |
const translate = key => { | |
return intl.formatMessage({ | |
id: key | |
}); | |
}; | |
return <Component {...compProps} t={translate} locale={intl.locale} />; | |
} | |
WithTranslate.displayName = `withTranslate(${Component.displayName || | |
Component.name}`; | |
WithTranslate.propTypes = { | |
intl: PropTypes.shape({ | |
locale: PropTypes.string.isRequired, | |
formatMessage: PropTypes.func.isRequired | |
}).isRequired | |
}; | |
return injectIntl(WithTranslate); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment