Created
April 27, 2016 10:40
-
-
Save erikras/647f4559b53d98e4c00684cced37ac19 to your computer and use it in GitHub Desktop.
The HOC Drill Pattern [AFTER]
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
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> | |
) | |
} | |
} |
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
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