Skip to content

Commit

Permalink
Merge pull request #4366 from kidroca/kidroca/locale-perf-fix
Browse files Browse the repository at this point in the history
(cherry picked from commit 7d842b9)
  • Loading branch information
iwiznia authored and OSBotify committed Aug 3, 2021
1 parent 9f31e72 commit 2fd926a
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/components/withLocalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ const localeProviderDefaultProps = {
};

class LocaleContextProvider extends React.Component {
constructor(props) {
super(props);

/* The context this component exposes to consumers */
this.translateUtils = {
/**
* The context this component exposes to child components
* @returns {object} translation util functions and locale
*/
getContextValue() {
return {
translate: this.translate.bind(this),
numberFormat: this.numberFormat.bind(this),
timestampToRelative: this.timestampToRelative.bind(this),
timestampToDateTime: this.timestampToDateTime.bind(this),
fromLocalPhone: this.fromLocalPhone.bind(this),
toLocalPhone: this.toLocalPhone.bind(this),
locale: this.props.preferredLocale,
};
}

Expand Down Expand Up @@ -115,7 +117,7 @@ class LocaleContextProvider extends React.Component {

render() {
return (
<LocaleContext.Provider value={this.translateUtils}>
<LocaleContext.Provider value={this.getContextValue()}>
{this.props.children}
</LocaleContext.Provider>
);
Expand All @@ -137,7 +139,7 @@ export default function withLocalize(WrappedComponent) {
const WithLocalize = forwardRef((props, ref) => (
<LocaleContext.Consumer>
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
{ translateUtils => <WrappedComponent {...translateUtils} {...props} ref={ref} />}
{translateUtils => <WrappedComponent {...translateUtils} {...props} ref={ref} />}
</LocaleContext.Consumer>
));

Expand Down

0 comments on commit 2fd926a

Please sign in to comment.