Skip to content

Instantly share code, notes, and snippets.

@bradparker
Last active January 18, 2016 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradparker/11efef2fb9ac1a3b508e to your computer and use it in GitHub Desktop.
Save bradparker/11efef2fb9ac1a3b508e to your computer and use it in GitHub Desktop.
HOC examples
// I18n
const translateable = (translations) => (Component) =>
(props = { locale: 'default' }) => {
const localTranslations = translations[locale]
return <Component { ...props } { ...localTranslations } />
}
const Greeter = ({ greeting, name }) =>
<p>{ greeting }, { name }</p>
const TranslatableGreeter = translateable({
default: {
greeting: 'Hello'
},
au: {
greeting: 'G\'day'
}
})(Greeter)
// Or more (sort of) explicitly
const Greeter = translateable({
default: { greeting: 'Hello' },
au: { greeting: 'G\'day' }
})(
(props = {greeting, name: 'There' }) => {
return <p>{ greeting }, { name }</p>
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment