Skip to content

Instantly share code, notes, and snippets.

@theptrk
Created August 7, 2015 06:27
Show Gist options
  • Save theptrk/62be7a77eb75eb3f8e50 to your computer and use it in GitHub Desktop.
Save theptrk/62be7a77eb75eb3f8e50 to your computer and use it in GitHub Desktop.
bindContext takes an array and binds a context
export default function (arr, context) {
arr.forEach(function (fn) {
context[fn] = context[fn].bind(context);
}.bind(context));
}
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