Skip to content

Instantly share code, notes, and snippets.

@graham
Created August 10, 2017 18:01
Show Gist options
  • Save graham/cea54635622e7056ad7d6bccf649406a to your computer and use it in GitHub Desktop.
Save graham/cea54635622e7056ad7d6bccf649406a to your computer and use it in GitHub Desktop.
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