## 1. Simple usage ```js class MessageList extends React.Component { //(2)Add getChildContext getChildContext() { return {color: "purple"}; } render() { const color = "purple"; const children = this.props.messages.map((message) => ); return
{children}<\/div>; } } //(1)Add MessageList.childContextTypes MessageList.childContextTypes = { color: PropTypes.string }; ``` We need to focus on *childContextTypes* and *getChildContext*. [Demo click here](./simpleFunc/readme.md) ## 2.Referencing Context in Lifecycle Methods If contextTypes is defined within a component, the following lifecycle methods will receive an additional parameter, the context object:
constructor(props, context)
componentWillReceiveProps(nextProps, nextContext)
shouldComponentUpdate(nextProps, nextState, nextContext)
componentWillUpdate(nextProps, nextState, nextContext)
componentDidUpdate(prevProps, prevState, prevContext)
## 3.Referencing Context in Stateless Functional Components Stateless functional components are also able to reference context if contextTypes is defined as a property of the function. The following code shows a Button component written as a stateless functional component. ```js const PropTypes = require('prop-types'); //context is second parameter of stateless function constructor const Button = ({children}, context) =>