Welcome to stack overflow :)
Context is mainly used by library authors, such as React Router and Redux - two widely used React libraries - currently rely on context. Here is a good summary written by Dan Abramov (author of Redux): fooobar.com/questions/89214 / ...
- , :
static contextTypes = {
prop: React.PropTypes.bool
};
: http://codepen.io/PiotrBerebecki/pen/LRLgJP, - .
class App extends React.Component {
static childContextTypes = {
prop: React.PropTypes.bool
};
getChildContext() {
return {prop: true};
}
render() {
return <MenuBar/ >;
}
}
class MenuBar extends React.Component {
constructor(props, context) {
super(props, context);
console.log('in constructor', context.prop) // logs: true
}
static contextTypes = {
prop: React.PropTypes.bool
};
render() {
console.log('in render', this.context.prop) // logs: true
return (
<div>MenuBar</div>
);
}
}
ReactDOM.render(
<App />,
document.getElementById('app')
);
React v15.5 PropTypes React.PropTypes PropTypes :
import PropTypes from 'prop-types';
. https://reactjs.org/docs/typechecking-with-proptypes.html.