Skip to content

Instantly share code, notes, and snippets.

@hongkheng
Last active September 20, 2020 05:14
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 hongkheng/01c19e3ad1cf35fa2b1805b5701b4ed8 to your computer and use it in GitHub Desktop.
Save hongkheng/01c19e3ad1cf35fa2b1805b5701b4ed8 to your computer and use it in GitHub Desktop.
React Higher Order Components
// 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