# Component State

URL: https://ark-ui.com/docs/guides/component-state
Source: https://raw.githubusercontent.com/chakra-ui/ark/refs/heads/main/website/src/content/pages/guides/component-state.mdx

Learn how to manage component state using Context and Provider components.

---

Need to access a component's state? You have three options:

| Approach                        | When to use it                                |
| ------------------------------- | --------------------------------------------- |
| `Component.Context`             | Quick inline access via named children        |
| `use*Context` hooks             | Build custom child components that read state |
| `useComponent` + `RootProvider` | Control the component from outside            |

## Context Components

Use `Component.Context` to access state inline via named children. Here, `Avatar.Fallback` reads the `loaded` state to
show different content:

<ExampleCode id="context" component="avatar" />

## Context Hooks

Every component exports a `use*Context` hook (like `useDialogContext` or `useMenuContext`). Call it from any child
component to access state and methods—no render props needed.

```ripple
import { Dialog, useDialogContext } from 'ark-ripple/dialog'

component CustomCloseButton() {
  const dialog = useDialogContext()
  
  <button onClick={() => dialog.setOpen(false)}>
    {"Close "} {dialog.open ? 'open' : 'closed'}
  </button>
}

export component Demo() {
  <Dialog.Root>
    <Dialog.Trigger>Open</Dialog.Trigger>
    <Dialog.Content>
      <CustomCloseButton />
    </Dialog.Content>
  </Dialog.Root>
}
```

The hook returns the same API as `Component.Context`, just without the nesting.

## Provider Components

Need to control a component from outside its tree? Use a `useComponent` hook (like `useDialog`) with `RootProvider`.

> When you use `RootProvider`, skip the `Root` component—you don't need both.

<ExampleCode id="root-provider" component="accordion" />

## Choosing the Right Approach

- **`Component.Context`** — Quick inline access for conditional rendering
- **`use*Context` hooks** — Build reusable child components that need parent state
- **`useComponent` + `RootProvider`** — Trigger actions from outside (like opening a dialog from a menu item)