Skip to content

Instantly share code, notes, and snippets.

@tmbtech
Created May 14, 2015 17:32
Show Gist options
  • Save tmbtech/4ec987163042374955e8 to your computer and use it in GitHub Desktop.
Save tmbtech/4ec987163042374955e8 to your computer and use it in GitHub Desktop.
ES7 Decorator Test
import React from "react";
import {ContextTypes, DefaultProps, PropTypes} from "./Decorators";
// this will throw a warning.
@PropTypes({name: React.PropTypes.func})
@DefaultProps({name: "robbie"})
class BaseClass extends React.Component {
render() {
return (
<div className="base-class-example">
<p>This is the base class example</p>
<p>My name is {this.props.name}</p>
</div>
);
}
}
export default BaseClass;
function _setOrExtend(source, property, values) {
if (source.hasOwnProperty(property)) {
Object.assign(source[property], values);
} else {
source[property] = values;
}
return source;
}
function ContextTypes(contextTypes = {}) {
return function (component) {
return _setOrExtend(component, "contextTypes", contextTypes);
};
}
function DefaultProps(defaultProps = {}) {
return function (component) {
return _setOrExtend(component, "defaultProps", defaultProps);
};
}
function PropTypes(propTypes = {}) {
return function (component) {
return _setOrExtend(component, "propTypes", propTypes);
};
}
export {ContextTypes, DefaultProps, PropTypes};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment