Skip to content

Instantly share code, notes, and snippets.

View choonkending's full-sized avatar

Ken Ding choonkending

View GitHub Profile
import React from 'react';
const showHide = (Title, Content) => class extends React.Component {
constructor(props) {
super(props);
this.state = { isActive: false };
this.onToggle = this.onToggle.bind(this);
}
render() {
import React from 'react';
const toggle = (ComposedComponent) => class extends React.Component {
constructor(props) {
super(props);
this.state = { isActive: false };
this.onToggle = this.onToggle.bind(this);
}
render() {
import React, { Component, PropTypes } from 'react';
export default class Toggle extends Component {
constructor(props) {
super(props);
this.state = { isActive: false };
this.onToggle = this.onToggle.bind(this);
}
render() {
/*
* Comparing arrow functions' lexical this binding to ordinary functions
*/
function Dog() {
// The arrow function binds this to the new Dog instance
// It is the same as writing it as
// const doSomething = function() { this.bark(); }.bind(this);
const doSomething = () => { this.bark() };
doSomething();
}
@choonkending
choonkending / thisChanges.js
Created September 21, 2016 05:05
how this used to be used
button.addEventListener('click', function(e) {
// Something
}.bind(this));
button.addEventListener('click', e => {
// Something
});
class ViewModel {
constructor(prop1, prop2) {
// https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md
/*
* Called with new? Use the newly constructed object
*/
class Pokemon {
constructor(hp, mp) {
this.hp = hp;
this.mp = mp;
}
}
/*
* Backbone view
*/
var View = Backbone.View.extend({
modifyDOM: function() {
this.$el.html(this.template(obj));
},
render: function() {
this.$el.html(this.template(obj));
@choonkending
choonkending / separationOfConcerns.js
Created September 21, 2016 08:55
An example of the evolution of a component
/* Start with building the component */
import React from 'react';
class Value extends React.Component {
constructor(props) {
super(props);
this.state = { isActive: false };
this.onToggle = this.onToggle.bind(this);
}
/* Modal */
<div className="modal">
<CloseButton />
</div>
/* CloseButton */
<button className="close-button">Close</button>
const createEventHandlers = (el = document, eventName, handler) => ({
add: () => { el.addEventListener(eventName, handler); },
remove: () => { el.removeEventListener(eventName, handler); }
});
/*
* In component
*/