Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
fix(translatable): avoid create a new function on every render (#1383)
Browse files Browse the repository at this point in the history
  • Loading branch information
samouss authored Jun 25, 2018
1 parent 7ced14c commit 1285b3b
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/react-instantsearch-core/src/core/translatable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { Component } from 'react';
import { has } from 'lodash';

const withKeysPropType = keys => (props, propName, componentName) => {
Expand All @@ -18,20 +18,25 @@ const withKeysPropType = keys => (props, propName, componentName) => {

export default function translatable(defaultTranslations) {
return Composed => {
function Translatable(props) {
const { translations, ...otherProps } = props;
const translate = (key, ...params) => {
class Translatable extends Component {
translate = (key, ...params) => {
const { translations } = this.props;

const translation =
translations && has(translations, key)
? translations[key]
: defaultTranslations[key];

if (typeof translation === 'function') {
return translation(...params);
}

return translation;
};

return <Composed translate={translate} {...otherProps} />;
render() {
return <Composed translate={this.translate} {...this.props} />;
}
}

const name = Composed.displayName || Composed.name || 'UnknownComponent';
Expand Down

0 comments on commit 1285b3b

Please sign in to comment.