---
title: "UndoState"
description: "Controls the undo and redo history for a [TextFieldState]."
type: "class"
lastmod: "2026-09-24"
---
## API Reference

> Source set: Common

```kotlin
public class UndoState internal constructor(private val state: TextFieldState)
```

Controls the undo and redo history for a [TextFieldState](/jetpack-compose/androidx.compose.foundation/foundation/classes/TextFieldState/api).

## Properties

### canUndo

> Source set: Common

```kotlin
public val canUndo: Boolean
```

Whether an `undo` action can currently be performed.

If this value is `false`, calling `undo` is a no-op. This property is backed by snapshot
state and will cause recomposition when its value changes.

### canRedo

> Source set: Common

```kotlin
public val canRedo: Boolean
```

Whether a `redo` action can currently be performed.

If this value is `false`, calling `redo` is a no-op. This property is backed by snapshot
state and will cause recomposition when its value changes.

## Functions

### undo

> Source set: Common

```kotlin
public fun undo()
```

Reverts the latest edit action or a group of actions that are merged together.

If `canUndo` is `false`, this is a no-op. Calling it repeatedly continues undoing previous
actions.

### redo

> Source set: Common

```kotlin
public fun redo()
```

Re-applies a change that was previously reverted via `undo`.

If `canRedo` is `false`, this is a no-op.

### clearHistory

> Source set: Common

```kotlin
public fun clearHistory()
```

Clears all undo and redo history up to this point.

Calling this sets both `canUndo` and `canRedo` to `false`.

## Code Examples

### BasicTextFieldUndoSample

```kotlin
@Sampled
@Composable
fun BasicTextFieldUndoSample() {
    val state = rememberTextFieldState()

    Column(Modifier.padding(8.dp)) {
        Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
            Button(onClick = { state.undoState.undo() }, enabled = state.undoState.canUndo) {
                Text("Undo")
            }

            Button(onClick = { state.undoState.redo() }, enabled = state.undoState.canRedo) {
                Text("Redo")
            }

            Button(
                onClick = { state.undoState.clearHistory() },
                enabled = state.undoState.canUndo || state.undoState.canRedo,
            ) {
                Text("Clear History")
            }
        }

        BasicTextField(
            state = state,
            modifier =
                Modifier.fillMaxWidth()
                    .border(1.dp, Color.LightGray, RoundedCornerShape(6.dp))
                    .padding(8.dp),
            textStyle = TextStyle(fontSize = 16.sp),
        )
    }
}
```
