Last active
September 27, 2017 09:06
-
-
Save victorhqc/de0d6a1ee50fb99a244e2f9f1a638838 to your computer and use it in GitHub Desktop.
High Order Component Implementation example
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, { 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