-
-
Save pyadav/d3ca4d2235f123f5f0c0 to your computer and use it in GitHub Desktop.
ES7 Decorator Test
This file contains 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 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; |
This file contains 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
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