Ark Logo
DocsExamplesShowcasePlus
React
Solid
Vue
GitHub
Guides
Component state
Ark Logo
    • Introduction
    • Getting Started
    • Changelog
    • About
    • Animation
    • Closed Components
    • Component State
    • Composition
    • Styling
    • Accordion
    • Avatar
    • Carousel
      preview
    • Checkbox
    • Clipboard
    • Collapsible
    • Color Picker
    • Combobox
    • Date Picker
    • Dialog
    • Editable
    • Field
    • Fieldset
    • File Upload
    • Hover Card
    • Menu
    • Number Input
    • Pagination
    • Pin Input
    • Popover
    • Presence
    • Progress - Circular
    • Progress - Linear
    • QR Code
      preview
    • Radio Group
    • Rating Group
    • Segment Group
    • Select
    • Signature Pad
      preview
    • Slider
    • Splitter
    • Switch
    • Tabs
    • Tags Input
    • Toast
    • Toggle Group
    • Tooltip
    • Tree View
      preview
    • Environment
    • Locale

Component State

Learn how to access the state of a component using Context and Provider components.

Motivation

Effectively accessing and managing component state is crucial for dynamic UIs in Ark. Using Context and Provider components, you can write modular, maintainable code. This guide shows how to share state and behavior across your application, enabling flexible and powerful component compositions.

Context Components

Context components allow you to access a component's state or functions from a child component. In the example below, Avatar.Fallback conditionally renders based on the loaded state.

import { Avatar } from '@ark-ui/react'

export const Context = () => (
  <Avatar.Root>
    <Avatar.Context>
      {(avatar) => <Avatar.Fallback>{avatar.loaded ? 'PA' : 'Loading'}</Avatar.Fallback>}
    </Avatar.Context>
    <Avatar.Image src="https://i.pravatar.cc/300" alt="avatar" />
  </Avatar.Root>
)
import { Avatar } from '@ark-ui/solid'

export const Context = () => (
  <Avatar.Root>
    <Avatar.Context>
      {(avatar) => <Avatar.Fallback>{avatar().loaded ? 'PA' : 'Loading'}</Avatar.Fallback>}
    </Avatar.Context>
    <Avatar.Image src="https://i.pravatar.cc/300" alt="avatar" />
  </Avatar.Root>
)
<script setup lang="ts">
import { Avatar } from '@ark-ui/vue'
</script>

<template>
  <Avatar.Root>
    <Avatar.Context v-slot="avatar">
      <Avatar.Fallback>
        {{ avatar.loaded ? 'PA' : 'Loading' }}
      </Avatar.Fallback>
    </Avatar.Context>
    <Avatar.Image src="https://i.pravatar.cc/300" alt="avatar" />
  </Avatar.Root>
</template>

Good to know (RSC): Due to the usage of render prop, you might need to add the 'use client' directive at the top of your file when using React Server Components.

Provider Components

Provider components can help coordinate state and behavior between multiple components, enabling interactions that aren't possible with Context components alone.

import { Avatar, useAvatar } from '@ark-ui/react'

export const Provider = () => {
  const avatar = useAvatar({
    onStatusChange: (e) => console.log('status changed', e),
  })

  return (
    <Avatar.RootProvider value={avatar}>
      <Avatar.Fallback>PA</Avatar.Fallback>
      <Avatar.Image src="https://i.pravatar.cc/300" alt="avatar" />
    </Avatar.RootProvider>
  )
}
import { Avatar, useAvatar } from '@ark-ui/solid'

export const Provider = () => {
  const avatar = useAvatar({
    onStatusChange: (e) => console.log('status changed', e),
  })

  return (
    <Avatar.RootProvider value={avatar}>
      <Avatar.Fallback>PA</Avatar.Fallback>
      <Avatar.Image src="https://i.pravatar.cc/300" alt="avatar" />
    </Avatar.RootProvider>
  )
}
<script setup lang="ts">
import { Avatar, useAvatar } from '@ark-ui/vue'

const avatar = useAvatar({
  onStatusChange: (e) => console.log('status changed', e),
})
</script>

<template>
  <Avatar.RootProvider :value="avatar">
    <Avatar.Fallback>PA</Avatar.Fallback>
    <Avatar.Image src="https://i.pravatar.cc/3000" alt="avatar" />
  </Avatar.RootProvider>
</template>

For more examples of composing components with Context and Provider components, visit our Examples section.

Prev page

Closed Components

Next page

Composition

On this page

components

Accordion

Component

A collapsible component for displaying content in a vertical stack.

Avatar

Component

A graphical representation of the user, often used in profile sections.

Carousel

Component

A slideshow component that cycles through elements.

Checkbox

Component

A control element that allows for multiple selections within a set.

Clipboard

Component

A component to copy text to the clipboard

Collapsible

Component

An interactive component that can be expanded or collapsed.

Color Picker

Component

A component that allows users to select a color from a color picker.

Combobox

Component

A single input field that combines the functionality of a select and input.

Date Picker

Component

A component that allows users to select a date from a calendar.

Dialog

Component

A modal window that appears on top of the main content.

Editable

Component

A component that allows users to edit text in place.

Field

Component

Provides a flexible container for form inputs, labels, and helper text.

Fieldset

Component

A set of form controls optionally grouped under a common name.

File Upload

Component

A component that is used to upload multiple files.

Hover Card

Component

A card that appears when a user hovers over an element.

Menu

Component

A list of options that appears when a user interacts with a button.

Number Input

Component

A field that allows user input of numeric values.

Pagination

Component

A navigation component that allows users to browse through pages.

Pin Input

Component

For pin or verification codes with auto-focus transfer and masking options.

Popover

Component

An overlay that displays additional information or options when triggered.

Presence

Component

Helps control the rendering and unmounting of your content based on a given state.

Progress - Circular

Component

An element that shows either determinate or indeterminate progress.

Progress - Linear

Component

An element that shows either determinate or indeterminate progress.

QR Code

Component

A component that generates a QR code based on the provided data.

Radio Group

Component

Allows single selection from multiple options.

Rating Group

Component

Allows users to rate items using a set of icons.

Segment Group

Component

Organizes and navigates between sections in a view.

Select

Component

Displays a list of options for the user to pick from.

Signature Pad

Component

A component that allows users to draw a signature using a signature pad.

Slider

Component

A control element that allows for a range of selections.

Splitter

Component

A component that divides your interface into resizable sections

Switch

Component

A control element that allows for a binary selection.

Tabs

Component

Flexible navigation tool with various modes and features.

Tags Input

Component

A component that allows users to add tags to an input field.

Toast

Component

A message that appears on the screen to provide feedback on an action.

Toggle Group

Component

A set of two-state buttons that can be toggled on or off.

Tooltip

Component

A label that provides information on hover or focus.

Tree View

Component

A component that is used to show a tree hierarchy.

examples
components

Accordion

Component

A collapsible component for displaying content in a vertical stack.

Avatar

Component

A graphical representation of the user, often used in profile sections.

Carousel

Component

A slideshow component that cycles through elements.

Checkbox

Component

A control element that allows for multiple selections within a set.

Clipboard

Component

A component to copy text to the clipboard

Collapsible

Component

An interactive component that can be expanded or collapsed.

Color Picker

Component

A component that allows users to select a color from a color picker.

Combobox

Component

A single input field that combines the functionality of a select and input.

Date Picker

Component

A component that allows users to select a date from a calendar.

Dialog

Component

A modal window that appears on top of the main content.

Editable

Component

A component that allows users to edit text in place.

Field

Component

Provides a flexible container for form inputs, labels, and helper text.

Fieldset

Component

A set of form controls optionally grouped under a common name.

File Upload

Component

A component that is used to upload multiple files.

Hover Card

Component

A card that appears when a user hovers over an element.

Menu

Component

A list of options that appears when a user interacts with a button.

Number Input

Component

A field that allows user input of numeric values.

Pagination

Component

A navigation component that allows users to browse through pages.

Pin Input

Component

For pin or verification codes with auto-focus transfer and masking options.

Popover

Component

An overlay that displays additional information or options when triggered.

Presence

Component

Helps control the rendering and unmounting of your content based on a given state.

Progress - Circular

Component

An element that shows either determinate or indeterminate progress.

Progress - Linear

Component

An element that shows either determinate or indeterminate progress.

QR Code

Component

A component that generates a QR code based on the provided data.

Radio Group

Component

Allows single selection from multiple options.

Rating Group

Component

Allows users to rate items using a set of icons.

Segment Group

Component

Organizes and navigates between sections in a view.

Select

Component

Displays a list of options for the user to pick from.

Signature Pad

Component

A component that allows users to draw a signature using a signature pad.

Slider

Component

A control element that allows for a range of selections.

Splitter

Component

A component that divides your interface into resizable sections

Switch

Component

A control element that allows for a binary selection.

Tabs

Component

Flexible navigation tool with various modes and features.

Tags Input

Component

A component that allows users to add tags to an input field.

Toast

Component

A message that appears on the screen to provide feedback on an action.

Toggle Group

Component

A set of two-state buttons that can be toggled on or off.

Tooltip

Component

A label that provides information on hover or focus.

Tree View

Component

A component that is used to show a tree hierarchy.

examples
DocsExamplesShowcasePlus
React
Solid
Vue
GitHub
    • Introduction
    • Getting Started
    • Changelog
    • About
    • Animation
    • Closed Components
    • Component State
    • Composition
    • Styling
    • Accordion
    • Avatar
    • Carousel
      preview
    • Checkbox
    • Clipboard
    • Collapsible
    • Color Picker
    • Combobox
    • Date Picker
    • Dialog
    • Editable
    • Field
    • Fieldset
    • File Upload
    • Hover Card
    • Menu
    • Number Input
    • Pagination
    • Pin Input
    • Popover
    • Presence
    • Progress - Circular
    • Progress - Linear
    • QR Code
      preview
    • Radio Group
    • Rating Group
    • Segment Group
    • Select
    • Signature Pad
      preview
    • Slider
    • Splitter
    • Switch
    • Tabs
    • Tags Input
    • Toast
    • Toggle Group
    • Tooltip
    • Tree View
      preview
    • Environment
    • Locale