Skip to content

Instantly share code, notes, and snippets.

@malte-wessel
Last active October 5, 2016 18:07
Show Gist options
  • Save malte-wessel/6a9ab5def723d6818897c626ec5550a0 to your computer and use it in GitHub Desktop.
Save malte-wessel/6a9ab5def723d6818897c626ec5550a0 to your computer and use it in GitHub Desktop.
Melody with React/Inferno: Refs
import { createComponent } from 'melody-component';
import template from './template.twig';
export default createComponent({
componentDidMount() {
this.refs.button.addEventListener('click', this.handleClick);
},
componentWillUnmount() {
this.refs.button.removeEventListener('click', this.handleClick);
}
template: template
});
import { createClass } from 'react';
export default function createComponent(definition) {
const { template, ...rest } = definition;
if (!template) throw new Error('Please provide a template.');
return createClass({
...rest,
render() {
return template(this.props);
}
});
}
<div class="box">
<button ref="button">Click me</button>
</div>
// Compiled twig template
export default function(props) {
return (
<div className="box">
<button ref="button">Click me</button>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment