# How can I use context to selectively pass data down a component hierarchy? (2 approaches)

```javascript
// OLD APPROACH

import React, { Component } from 'react'
import PropTypes from 'prop-types'

class Chapter extends Component {
  static childContextTypes = {
    pageTitle: PropTypes.string.isRequired,        // (1) Define a `pageTitle` to add to context
    paragraphTitle: PropTypes.string.isRequired    // (2) Define a `paragraphTitle` to add to context
  }
  getChildContext = () => ({
      pageTitle: 'page title',                     // (3) Add `pageTitle` to context
      paragraphTitle: 'paragraph title'            // (4) Add `paragraphTitle` to context

  })
  render = () => <Page /> 
}
```

```jsx
import React, { Component } from 'react'
import PropTypes from 'prop-types'

class Page extends Component {
  static contextTypes = {
    pageTitle: PropTypes.string.isRequired         // (5) Expose `pageTitle` only to this component
  }
  constructor(props, context) {
    super(props)
    console.log('[x] context', context)            // (6) Context will only have `pageTitle` on it
  }
  render = () => {
    console.log('[x] this.context', this.context)  // (7) Context will only have `pageTitle` on it
    return <Paragraph />
  }  
}
```

```jsx
import React, { Component } from 'react'
import PropTypes from 'prop-types'

class Paragraph extends Component {
  static contextTypes = {
    paragraphTitle: PropTypes.string.isRequired   // (5) Expose `paragraphTitle` only to this component
  }
  constructor(props, context) {
    super(props)
    console.log('[x] context', context)           // (6) Context will only have `paragraphTitle` on it
  }
  render = () => {
    console.log('[x] this.context', this.context) // (7) Context will only have `paragraphTitle` on it
    return <p>'All Done'</p>
  }  
}
```

The `context` object passed into `Chapter` will have `pageTitle` and `paragraphTitle` added to it

The `context` object passed into `Page` will only contain \`pageTitle\` on it.

The `context` object passed into `Paragraph` will only contain `paragraphTitle` on it.

```jsx
/// NEW APPROACH

import React, { createContext } from 'react'

class MyContext = createContext()

class MyProvider extends Component {
    state = { name: 'henry' }
    render () => <MyContext.Provider>{this.props.children}</MyContext.Provider>
}

const App = () =>
    <MyProvider>
        <Family />
    </MyProvider>

const Family = () => 
    <div>
        <Person />
    </div>

const Person = () => 
    <MyContext.Consumer>
        {(context) => <p>{context.state.name}</p> }
    </MyContext.Consumer>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://asad-razvi.gitbook.io/javascript-cookbook/react-recipes/how-can-i-use-context-to-selectively-pass-data-down-a-component-hierarchy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
