# Context API

### Version 01. basic

```javascript
import React, { createContext } from 'react'

const Context = createContext()
const { Provider, Consumer: ApplicationConsumer } = Context


class ApplicationContextProvider extends React.Component {
    state = {
        color: 'blue'
    }

    actions = {
        setColor: (color) => {
            this.setState({ ...this.state, color })
        }
    }
    render() {
        const { state, actions, props: { children } } = this

        return (
            <Provider value={{ state, actions }}>
                {children}
            </Provider>
        )
    }
}

export {
    ApplicationConsumer,
    ApplicationContextProvider
}
```

```javascript
import React from 'react'
import { ApplicationContextProvider } from './components/application-context'
import Box from './components/box'

function App () {
  return (
    <ApplicationContextProvider>
      <div>
        <Box />
      </div>
    </ApplicationContextProvider>
  )
}

export default App
```

```javascript
import React from 'react'
import { ApplicationConsumer } from './application-context'

function Box() {
    return (
        <ApplicationConsumer>
            {
                (context) => {
                   console.log(context)
                   return (
                    <div>
                        {JSON.stringify(context)}
                    </div>
                   )
                }
            }
        </ApplicationConsumer>
    )
}

export default Box
```

### version 02. HOC

```javascript
import React, { createContext } from 'react'

const Context = createContext()
const { Provider, Consumer: ApplicationConsumer } = Context


class ApplicationContextProvider extends React.Component {
    state = {
        color: 'blue'
    }

    actions = {
        setColor: (color) => {
            this.setState({ ...this.state, color })
        }
    }
    render() {
        const { state, actions, props: { children } } = this

        return (
            <Provider value={{ state, actions }}>
                {children}
            </Provider>
        )
    }
}

function useApplicationContext(WrappedComponent) {
    return () => {
        return (
            <ApplicationConsumer>
                {
                    ({ state, actions }) => (
                        <WrappedComponent 
                            source={state}
                            actions={actions}
                        />
                    )
                }
            </ApplicationConsumer>
        )
    }
}

export {
    ApplicationConsumer,
    useApplicationContext,
    ApplicationContextProvider
}
```

```javascript
import React from 'react'
import { useApplicationContext } from './application-context'

function Box(props) {
    console.log(props)
    return (
        <div>
            {JSON.stringify(props)}
        </div>
    )
}

export default useApplicationContext(Box)
```

```javascript
import React from 'react'
import { useApplicationContext } from './application-context'

function Box({ actions, source }) {
    const { color } = source
    const { setColor } = actions
    
    return (
        <div>
            {color}
            <button onClick={() => setColor('red')}>색상변경</button>
        </div>
    )
}

export default useApplicationContext(Box)
```

### version 03. Hooks

```javascript
import React, { createContext, useContext, useState } from 'react'

const Context = createContext()

export function ApplicationContextProvider({ children }) {
    const [color, setColor] = useState('blue')

    return (
        <Context.Provider value={{
            color,
            setColor
        }}>
            {children}
        </Context.Provider>
    )
}

export function useApplicationContext() {
    return useContext(Context)
}
```

```javascript
import React from 'react'
import { ApplicationContextProvider } from './components/application-context'
import Box from './components/box'

function App () {
  return (
    <ApplicationContextProvider>
      <Box />
    </ApplicationContextProvider>
  )
}

export default App
```

```javascript
import React from 'react'
import { useApplicationContext } from './application-context'

function Box() {
    const { color, setColor } = useApplicationContext()

    return (
        <div>
            {color}
            <button onClick={() => setColor('red')}>색상변경</button>
        </div>
    )
}

export default Box
```


---

# 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://simplereact.gitbook.io/simplereact/context-api.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.
