> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/gradio-app/gradio/llms.txt
> Use this file to discover all available pages before exploring further.

# State

> Manage session state across function calls

The State component stores session state that persists across function calls for the same user.

## Basic usage

```python theme={null}
import gradio as gr

def increment(count):
    return count + 1, count + 1

with gr.Blocks() as demo:
    state = gr.State(value=0)
    display = gr.Number(label="Count")
    btn = gr.Button("Increment")
    
    btn.click(fn=increment, inputs=state, outputs=[display, state])
    
demo.launch()
```

## Constructor

<ParamField path="value" type="Any" default="None">
  Initial state value of any type. The value is deepcopied
</ParamField>

<ParamField path="time_to_live" type="int | float | None" default="None">
  Seconds to keep state after creation/update. If None, stored indefinitely
</ParamField>

<ParamField path="delete_callback" type="Callable[[Any], None] | None" default="None">
  Function called when state is deleted, receives state value as argument
</ParamField>

## Events

* **change** - Triggered when state value changes

## Examples

### Chat history

```python theme={null}
import gradio as gr

def chat(message, history):
    history = history + [(message, f"Echo: {message}")]
    return history, history

with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    state = gr.State(value=[])
    
    msg.submit(chat, [msg, state], [chatbot, state])
    
demo.launch()
```

### With TTL

```python theme={null}
import gradio as gr

# State expires after 1 hour of inactivity
state = gr.State(value={}, time_to_live=3600)
```

### With cleanup callback

```python theme={null}
import gradio as gr

def cleanup(value):
    print(f"Cleaning up state: {value}")
    # Perform cleanup operations

state = gr.State(value={}, delete_callback=cleanup)
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.