Skip to content

Instantly share code, notes, and snippets.

@developit
Created June 17, 2015 20:01
Show Gist options
  • Save developit/01660e4deac0a1bb79a4 to your computer and use it in GitHub Desktop.
Save developit/01660e4deac0a1bb79a4 to your computer and use it in GitHub Desktop.
React ES6 Components
import React from 'react';
export class Component extends React.Component {
constructor(...args) {
super(...args);
this.generateBindings();
if (typeof this.init==='function') {
this.init();
}
}
render() {
this.update(this.state, this.bindings);
}
generateBindings() {
this.binds = {};
for (let i in this) {
if (typeof this[i]==='function') {
this.binds[i] = this[i].bind(this);
}
}
return this.binds;
}
}
import { Component } from './es-component';
export class Example extends Component {
init() {
this.state = { foo:'bar' };
}
update({ foo }, { setFoo }) {
return <a onClick={ setFoo('baz') }>{ foo }</a>;
}
setFoo(foo) {
this.setState({ foo })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment