Created
December 29, 2016 12:09
-
-
Save shlomitc/99091a7db0057c57437f5890716a7d97 to your computer and use it in GitHub Desktop.
React context usage and overriding example
This file contains hidden or 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'; | |
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