Skip to content

Instantly share code, notes, and snippets.

@erikras
Created April 27, 2016 10:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikras/647f4559b53d98e4c00684cced37ac19 to your computer and use it in GitHub Desktop.
Save erikras/647f4559b53d98e4c00684cced37ac19 to your computer and use it in GitHub Desktop.
The HOC Drill Pattern [AFTER]
import React, { Component } from 'react'
import MyWrappedComponent from './MyWrappedComponent'
export default class MyContainer extends Component {
constructor() {
// bind handlers
this.handleCallback = this.handleCallback.bind(this)
}
handleCallback(load, save, clear) {
// save instance methods from MyWrappedComponent to this
this.handleLoad = load
this.handleSave = save
this.handleClear = clear
}
render() {
return (
<div>
<button onClick={this.handleLoad}>Load</button>
<button onClick={this.handleSave}>Save</button>
<button onClick={this.handleClear}>Clear</button>
<MyWrappedComponent methodCallback={this.handleCallback}/>
</div>
)
}
}
import React, { Component } from 'react'
import { hocLibrary } from 'some-hoc-library'
@hocLibrary({ config: 'parameters' })
export default class MyWrappedComponent extends Component {
componentWillMount() {
this.props.methodCallback(
this.load.bind(this),
this.save.bind(this),
this.clear.bind(this)
)
}
load() {
// load something
}
save() {
// save something
}
clear() {
// clear something
}
render() {
return <div>Gorgeous UI</div>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment