> Documentation index: https://developer.android.com/reference/llms.txt

# rememberTextFieldState

Common/All

## Functions summary

|---|---|---|
| `TextFieldState` | `@Composable rememberTextFieldState(initialText: String, initialSelection: TextRange)` Create and remember a `TextFieldState`. | Cmn |
| `TextFieldState` | `@Composable rememberTextFieldState( initialText: String, initialSelection: TextRange, onTextChanged: ((CharSequence) -> Unit)? )` Create and remember a `TextFieldState` that calls `onTextChanged` when its text changes. | Cmn |

## Functions

### rememberTextFieldState

Cmn Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation.md.txt) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/input/TextFieldState.kt+function:rememberTextFieldState) Added in [1.10.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation.md.txt#1.10.0)

```
@Composable
fun rememberTextFieldState(
    initialText: String = "",
    initialSelection: TextRange = TextRange(initialText.length)
): TextFieldState
```

Create and remember a `TextFieldState`. The state is remembered using `rememberSaveable` and so will be saved and restored with the composition.

If you need to store a `TextFieldState` in another object, use the `TextFieldState.Saver` object to manually save and restore the state.

| Parameters |
|---|---|
| `initialText: String = ""` | The initial text state. If a different value is passed in a subsequent recomposition, the value of the state will *not* be updated. To update the state after it's initialized, call methods on `TextFieldState`. |
| `initialSelection: TextRange = TextRange(initialText.length)` | The initial selection state. If a different value is passed in a subsequent recomposition, the value of the state will *not* be updated. To update the state after it's initialized, call methods on `TextFieldState`. |

### rememberTextFieldState

Cmn Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation.md.txt) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/input/TextFieldState.kt+function:rememberTextFieldState)

```
@Composable
fun rememberTextFieldState(
    initialText: String = "",
    initialSelection: TextRange = TextRange(initialText.length),
    onTextChanged: ((CharSequence) -> Unit)?
): TextFieldState
```

Create and remember a `TextFieldState` that calls `onTextChanged` when its text changes. The state is remembered using `rememberSaveable` and so will be saved and restored with the composition.

`onTextChanged` is called after each commit that changes the text. This includes user input, `TextFieldState.edit` and its helpers, and undo/redo. It is not called when only the selection, composition, or styles change.

`onTextChanged` runs on the thread that made the edit, before the edit call returns. When `onTextChanged` runs, `TextFieldState.text` and `TextFieldState.selection` show the new value, the undo entry is recorded, and the IME has been notified.

`onTextChanged` must not change the state. Calling `TextFieldState.edit`, its helpers, or `UndoState.undo` and `UndoState.redo` on the state from `onTextChanged` throws an `IllegalStateException`. To intercept and modify user input, use `InputTransformation`.

The `CharSequence` passed to `onTextChanged` is provided for convenience and has the same text content as `TextFieldState.text`. To read the selection, read `TextFieldState.selection` inside the callback.

The latest `onTextChanged` passed to this function is used after each recomposition. It is also passed to the state when the state is restored.

If you need to store a `TextFieldState` in another object, use `TextFieldState.Saver(onTextChanged)` to manually save and restore the state.

```kotlin
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.Text
import androidx.compose.runtime.remember

// The composable owns the state. onNameChanged is read again on every recomposition, so the
// latest lambda is always the one that runs.
val state = rememberTextFieldState(onTextChanged = { onNameChanged(it.toString()) })
BasicTextField(state)
```

| Parameters |
|---|---|
| `initialText: String = ""` | The initial text state. If a different value is passed in a subsequent recomposition, the value of the state will *not* be updated. To update the state after it's initialized, call methods on `TextFieldState`. |
| `initialSelection: TextRange = TextRange(initialText.length)` | The initial selection state. If a different value is passed in a subsequent recomposition, the value of the state will *not* be updated. To update the state after it's initialized, call methods on `TextFieldState`. |
| `onTextChanged: ((CharSequence) -> Unit)?` | Called after each commit that changes the text, receiving the new text with the same content as `TextFieldState.text`. |