Created
April 4, 2015 20:14
-
-
Save dignifiedquire/fe35f33e3973a6d4d5e1 to your computer and use it in GitHub Desktop.
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 "react"; | |
const Enhance = Target => class extends Component { | |
state = { | |
data: null | |
} | |
constructor() { | |
super() | |
} | |
componentDidMount() { | |
this.setState({ data: 'Hello' }) | |
} | |
render() { | |
return <Target {...this.props} data={this.state.data} /> | |
} | |
} | |
@Enhance | |
export class Button extends Component { | |
static propTypes = { | |
width: number | |
} | |
static defaultProps = { | |
width: 100 | |
} | |
// Initial State | |
state = { | |
counter: Math.round(this.props.width / 10) | |
} | |
_handleClick = (event) => { | |
event.preventDefault(); | |
this.setState({ counter: this.state.counter + 1 }); | |
} | |
render() { | |
return ( | |
<div> | |
This button has been clicked: {this.state.counter} times | |
Some cool data: {this.props.data} | |
<button | |
onClick={this._handleClick.bind(this)} | |
style={{ width: this.props.width }} /> | |
</div> | |
) | |
} | |
} |
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
"use strict"; | |
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; | |
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | |
var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; | |
var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; | |
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | |
Object.defineProperty(exports, "__esModule", { | |
value: true | |
}); | |
var _Component3 = require("react"); | |
var Enhance = function Enhance(Target) { | |
return (function (_Component) { | |
var _class = function () { | |
_classCallCheck(this, _class); | |
_get(Object.getPrototypeOf(_class.prototype), "constructor", this).call(this); | |
this.state = { | |
data: null | |
}; | |
}; | |
_inherits(_class, _Component); | |
_createClass(_class, [{ | |
key: "componentDidMount", | |
value: function componentDidMount() { | |
this.setState({ data: "Hello" }); | |
} | |
}, { | |
key: "render", | |
value: function render() { | |
return React.createElement(Target, _extends({}, this.props, { data: this.state.data })); | |
} | |
}]); | |
return _class; | |
})(_Component3.Component); | |
}; | |
var Button = (function (_Component2) { | |
function Button() { | |
var _this = this; | |
_classCallCheck(this, Button); | |
if (_Component2 != null) { | |
_Component2.apply(this, arguments); | |
} | |
this.state = { | |
counter: Math.round(this.props.width / 10) | |
}; | |
this._handleClick = function (event) { | |
event.preventDefault(); | |
_this.setState({ counter: _this.state.counter + 1 }); | |
}; | |
} | |
_inherits(Button, _Component2); | |
_createClass(Button, [{ | |
key: "render", | |
value: function render() { | |
return React.createElement( | |
"div", | |
null, | |
"This button has been clicked: ", | |
this.state.counter, | |
" times Some cool data: ", | |
this.props.data, | |
React.createElement("button", { | |
onClick: this._handleClick.bind(this), | |
style: { width: this.props.width } }) | |
); | |
} | |
}], [{ | |
key: "propTypes", | |
enumerable: true, | |
value: { | |
width: number | |
} | |
}, { | |
key: "defaultProps", | |
enumerable: true, | |
value: { | |
width: 100 | |
} | |
}]); | |
exports.Button = Button = Enhance(Button) || Button; | |
return Button; | |
})(_Component3.Component); | |
exports.Button = Button; | |
// Initial State |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment