Skip to content

useContext

Александр edited this page Jan 2, 2024 · 2 revisions
export function useContext<T>(context: React.Context<T>): () => T

Accepts a context object.

Subscribes to context changes and returns contextGetter.

import { afc, useContext } from 'react-afc'
import { NameContext } from './NameContext'

function Component(props) {
  const getContext = useContext(NameContext)

  function greet() {
    const name = getContext()
    return `Hi, ${name}!`
  }

  return () => (
    <p>{greet()}</p>
  )
}

export default afc(Component)
Clone this wiki locally