Created
June 17, 2015 20:01
-
-
Save developit/01660e4deac0a1bb79a4 to your computer and use it in GitHub Desktop.
React ES6 Components
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'; | |
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; | |
} | |
} |
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 { 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