Skip to content

Instantly share code, notes, and snippets.

@KSXGitHub
Last active February 1, 2018 06:17
Show Gist options
  • Save KSXGitHub/49d50eacc71024fa2434978aff09cf7f to your computer and use it in GitHub Desktop.
Save KSXGitHub/49d50eacc71024fa2434978aff09cf7f to your computer and use it in GitHub Desktop.
Higher order component/function
class MyComponent extends React.Component {
render () {
const {Foo, Bar, children} = this.props
return <Foo hello='world'>
<Bar>{...children}</Bar>
</Foo>
}
}
// declare a higher order component
function MyComponent (props) {
const {Foo, Bar} = props
return <Foo hello='world'>
<Bar />
</Foo>
}
// use it
const mycomponent = <MyComponent
Foo={MyFooComponent}
Bar={MyBarComponent}
/>
// Although this is not React, concept higher order component comes from this: Higher Order Function
// declare a higher order function
const compose = (outer, inner) => x => outer(inner(x))
// use it
const reverse = str => Array.from(str).reverse().join('')
const uppercase = str => str.toUpperCase()
const reverseThenUppercase = compose(reverse, uppercase) // <- compose is a higher order function
console.log(reverseThenUppercase('abcdef')) // should print 'FEDCBA'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment