Last active
January 18, 2016 05:41
-
-
Save bradparker/11efef2fb9ac1a3b508e to your computer and use it in GitHub Desktop.
HOC examples
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
// 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