Last active
February 1, 2018 06:17
-
-
Save KSXGitHub/49d50eacc71024fa2434978aff09cf7f to your computer and use it in GitHub Desktop.
Higher order component/function
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
class MyComponent extends React.Component { | |
render () { | |
const {Foo, Bar, children} = this.props | |
return <Foo hello='world'> | |
<Bar>{...children}</Bar> | |
</Foo> | |
} | |
} |
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
// 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} | |
/> |
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
// 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