Skip to content

Instantly share code, notes, and snippets.

@victorhqc
Last active September 27, 2017 09:06
Show Gist options
  • Save victorhqc/de0d6a1ee50fb99a244e2f9f1a638838 to your computer and use it in GitHub Desktop.
Save victorhqc/de0d6a1ee50fb99a244e2f9f1a638838 to your computer and use it in GitHub Desktop.
High Order Component Implementation example
import React, { Component } from 'react';
import {
translateFromCountry,
} from './translate';
// High order Component translate
const translate = (WrappedComponent) => {
return class extends Component {
translate = (key, params) => {
const {
country,
} = this.props;
return translateFromCountry(country)(key, params);
}
render() {
return (
<WrappedComponent {...this.props} translate={this.translate} />
);
}
}
}
// wrapped component by HoC translate
class MyComponent extends Component {
render() {
const {
translate,
} = this.props;
return (
<div>
{translate('welcome', { user: 'Victor' })}
</div>
)
}
}
export default translate(MyComponent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment