Created
August 10, 2017 18:01
-
-
Save graham/cea54635622e7056ad7d6bccf649406a to your computer and use it in GitHub Desktop.
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 * as React from 'react'; | |
import * as ReactDOM from 'react-dom'; | |
import * as PropTypes from 'prop-types'; | |
const styleObj = PropTypes.shape({ | |
color: PropTypes.string, | |
fontSize: PropTypes.number | |
}); | |
interface ButtonProps { | |
label: string; | |
} | |
class Button extends React.Component<ButtonProps, {}> { | |
static contextTypes: any = { | |
'style': styleObj | |
}; | |
handleClick = () => { | |
alert('click'); | |
} | |
render() { | |
return ( | |
<button | |
className="btn btn-default" | |
onClick={this.handleClick} | |
style={{ | |
'fontSize': this.context.style.fontSize + 'px', | |
'color': this.context.style.color | |
}}> | |
{this.props.label} | |
</button> | |
); | |
} | |
} | |
class MyApp extends React.Component<{}, {}> { | |
static childContextTypes: any = { | |
'style': styleObj, | |
}; | |
getChildContext() { | |
return { | |
style: { | |
color: 'blue', | |
fontSize: 20 | |
} | |
}; | |
} | |
render() { | |
return ( | |
<div> | |
<Button label={"click me"} /> | |
<Button label={"but also click me"} /> | |
<Button label={"click me"} /> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render( | |
<MyApp />, | |
document.getElementById('content') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment