Skip to content

Instantly share code, notes, and snippets.

@shlomitc
Created December 29, 2016 12:09
Show Gist options
  • Save shlomitc/99091a7db0057c57437f5890716a7d97 to your computer and use it in GitHub Desktop.
Save shlomitc/99091a7db0057c57437f5890716a7d97 to your computer and use it in GitHub Desktop.
React context usage and overriding example
import React from 'react';
class A extends React.Component {
render() {
return (
<div>
<h1>A</h1>
<B/>
</div>
);
}
static childContextTypes = {
given: React.PropTypes.string
};
getChildContext() {
return {
given: 'A'
};
}
}
class B extends React.Component {
render() {
return (
<div>
<h1>B from {this.context.given}</h1>
<C/>
</div>
);
}
static contextTypes = {
given: React.PropTypes.string
};
static childContextTypes = {
given: React.PropTypes.string
};
getChildContext() {
return {
given: 'B'
};
}
}
class C extends React.Component {
render() {
return (
<div>
<h1>C from {this.context.given}</h1>
</div>
);
}
static contextTypes = {
given: React.PropTypes.string
};
}
export default A;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment