# observeAsState

## Functions summary

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<T?>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<T>.https://developer.android.com/reference/kotlin/androidx/compose/runtime/livedata/observeAsState.composable#(androidx.lifecycle.LiveData).observeAsState()()` Starts observing this `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` and represents its values via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<R>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, T : R> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<T>.https://developer.android.com/reference/kotlin/androidx/compose/runtime/livedata/observeAsState.composable#(androidx.lifecycle.LiveData).observeAsState(kotlin.Any)(initial: R)` Starts observing this `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` and represents its values via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. |

## Functions

### LiveData.observeAsState

Artifact: [androidx.compose.runtime:runtime-livedata](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/livedata/LiveDataAdapter.kt+function:observeAsState) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
@Composable
fun <T : Any?> LiveData<T>.observeAsState(): State<T?>
```

Starts observing this `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` and represents its values via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. Every time there would be new value posted into the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` will be updated causing recomposition of every `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` usage.

The inner observer will automatically be removed when this composable disposes or the current `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` moves to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state.

```kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.livedata.observeAsState

val value: String? by liveData.observeAsState()
Text("Value is $value")
```

### LiveData.observeAsState

Artifact: [androidx.compose.runtime:runtime-livedata](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/livedata/LiveDataAdapter.kt+function:observeAsState) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
@Composable
fun <R : Any?, T : R> LiveData<T>.observeAsState(initial: R): State<R>
```

Starts observing this `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` and represents its values via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. Every time there would be new value posted into the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` will be updated causing recomposition of every `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` usage.

The `https://developer.android.com/reference/kotlin/androidx/compose/runtime/livedata/package-summary#(androidx.lifecycle.LiveData).observeAsState(kotlin.Any)` value will be used only if this LiveData is not already `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#isInitialized()`. Note that if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/livedata/package-summary#(androidx.lifecycle.LiveData).observeAsState(kotlin.Any)` is a non-null type, it is your responsibility to ensure that any value you set on this LiveData is also non-null.

The inner observer will automatically be removed when this composable disposes or the current `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` moves to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state.

```kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.livedata.observeAsState

val value: String by liveData.observeAsState("initial")
Text("Value is $value")
```