Skip to content

Instantly share code, notes, and snippets.

@phusick
Last active February 9, 2016 10:21
Show Gist options
  • Save phusick/b20c4b361d8dfb65c178 to your computer and use it in GitHub Desktop.
Save phusick/b20c4b361d8dfb65c178 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<!-- put markup and other contents here -->
<div id="app"></div>
</body>
</html>
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
let Mixin = InnerComponent => class extends Component {
constructor() {
super();
this.update = this.update.bind(this);
this.state = { val: 0 };
}
update() {
this.setState({ val: this.state.val + 1 });
}
componentWillMount() {
console.log('will mount');
}
render() {
return <InnerComponent
update={this.update}
{...this.state}
{...this.props}
/>
}
componentDidMount() {
console.log('mounted');
}
}
const Button = (props) => <button
onClick={props.update}
>
{props.txt} - {props.val}
</button>;
const Label = (props) => <label
onMouseMove={props.update}>
{props.txt} - {props.val}
</label>;
let ButtonMixed = Mixin(Button);
let LabelMixed = Mixin(Label);
class App extends React.Component {
render(){
return (
<div>
<ButtonMixed txt="Button" />
<LabelMixed txt="Label" />
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"react": "0.14.7",
"react-dom": "0.14.7",
"babel-runtime": "6.3.19"
}
}
'use strict';
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Mixin = function Mixin(InnerComponent) {
return (function (_Component) {
(0, _inherits3.default)(_class, _Component);
function _class() {
(0, _classCallCheck3.default)(this, _class);
var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(_class).call(this));
_this.update = _this.update.bind(_this);
_this.state = { val: 0 };
return _this;
}
(0, _createClass3.default)(_class, [{
key: 'update',
value: function update() {
this.setState({ val: this.state.val + 1 });
}
}, {
key: 'componentWillMount',
value: function componentWillMount() {
console.log('will mount');
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement(InnerComponent, (0, _extends3.default)({
update: this.update
}, this.state, this.props));
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
console.log('mounted');
}
}]);
return _class;
})(_react.Component);
};
var Button = function Button(props) {
return _react2.default.createElement(
'button',
{
onClick: props.update
},
props.txt,
' - ',
props.val
);
};
var Label = function Label(props) {
return _react2.default.createElement(
'label',
{
onMouseMove: props.update },
props.txt,
' - ',
props.val
);
};
var ButtonMixed = Mixin(Button);
var LabelMixed = Mixin(Label);
var App = (function (_React$Component) {
(0, _inherits3.default)(App, _React$Component);
function App() {
(0, _classCallCheck3.default)(this, App);
return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(App).apply(this, arguments));
}
(0, _createClass3.default)(App, [{
key: 'render',
value: function render() {
return _react2.default.createElement(
'div',
null,
_react2.default.createElement(ButtonMixed, { txt: 'Button' }),
_react2.default.createElement(LabelMixed, { txt: 'Label' })
);
}
}]);
return App;
})(_react2.default.Component);
_reactDom2.default.render(_react2.default.createElement(App, null), document.getElementById('app'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment