Last active
September 20, 2020 05:14
-
-
Save hongkheng/01c19e3ad1cf35fa2b1805b5701b4ed8 to your computer and use it in GitHub Desktop.
React Higher Order Components
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
// HOC FactoryFactory method | |
function HOCFactoryFactory (...params) { | |
console.log('params', params) | |
return function HOCFactory (WrappedComponent) { | |
return class withHOCFactory extends React.Component { | |
render () { | |
return <WrappedComponent {...this.props} /> | |
} | |
} | |
} | |
} | |
// Testing HOC | |
function withEnhance (ComposedComponent) { | |
return class withEnhance extends React.Component { | |
render () { | |
console.log('ComposedC', this.props) | |
return <ComposedComponent {...this.props} /> | |
} | |
} | |
} | |
function WrappedComponent (props) { | |
return <div>I am Enhanced! <button onClick={props.onSubmit}>Clicked</button></div> | |
} | |
function testExposed () { | |
return { | |
someProps: 'exposed!' | |
} | |
} | |
const factoryProps = { | |
testing: 'hi', | |
override: testExposed(), | |
onSubmit: (e) => { | |
console.log('passing it down') | |
} | |
} | |
const FactoryMade = HOCFactoryFactory(factoryProps)(WrappedComponent) | |
const EnhancedComponent = withEnhance(WrappedComponent) | |
// arrow functions syntax | |
const Loader = ({ size }) => ( | |
<div>Spinner</div> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment