Created
August 7, 2015 06:27
-
-
Save theptrk/62be7a77eb75eb3f8e50 to your computer and use it in GitHub Desktop.
bindContext takes an array and binds a context
This file contains hidden or 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
export default function (arr, context) { | |
arr.forEach(function (fn) { | |
context[fn] = context[fn].bind(context); | |
}.bind(context)); | |
} |
This file contains hidden or 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 from 'react' | |
import bindContext from './path/to/bindContext'; | |
export default class SomeComponent extends React.Component { | |
constructor() { | |
super(); | |
// ========== Too verbose ========== | |
// this.toggleEventForm = this.toggleEventForm.bind(this); | |
// this.handleSubmit = this.handleSubmit.bind(this); | |
// this.handleChange = this.handleChange.bind(this); | |
// ========== Ugly ========== | |
// [ | |
// 'toggleEventForm', | |
// 'handleChange', | |
// 'handleSubmit' | |
// ].forEach(function (fn) { | |
// this[fn] = this[fn].bind(this); | |
// }.bind(this)); | |
// ========== Prettify your context bindings ========== | |
_bindContext([ | |
'toggleEventForm', | |
'handleChange', | |
'handleSubmit' | |
], this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment