Skip to content

Instantly share code, notes, and snippets.

@muromec
Created January 28, 2016 17:58
Show Gist options
  • Save muromec/cd2063442dbd16526af2 to your computer and use it in GitHub Desktop.
Save muromec/cd2063442dbd16526af2 to your computer and use it in GitHub Desktop.
react contexts (0.13)
import React from 'react';
class Inside extends React.Component {
static contextTypes = {
section: React.PropTypes.string,
};
render() {
return (<span>INSIDE {this.context.section}</span>);
}
}
class Wrapper extends React.Component {
static childContextTypes = {
section: React.PropTypes.string,
};
getChildContext() {
return {
section: '123',
};
}
render() {
return (<div>
{React.Children.map(
this.props.children,
(element)=> (
React.createElement(element.type, element.props)
)
)}
</div>);
}
}
export default class Context extends React.Component {
static childContextTypes = {
section: React.PropTypes.string,
};
getChildContext() {
return {
section: '333',
};
}
render() {
return (<Wrapper>
<Inside />
</Wrapper>);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment