# androidx.compose.runtime.livedata

# androidx.compose.runtime.livedata

## Extension functions summary

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

## Extension functions

### 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[LiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData)and represents its values via[State](https://developer.android.com/reference/kotlin/androidx/compose/runtime/State). Every time there would be new value posted into the[LiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData)the returned[State](https://developer.android.com/reference/kotlin/androidx/compose/runtime/State)will be updated causing recomposition of every[State.value](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[LifecycleOwner](https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)moves to the[Lifecycle.State.DESTROYED](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")
```  

### 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[LiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData)and represents its values via[State](https://developer.android.com/reference/kotlin/androidx/compose/runtime/State). Every time there would be new value posted into the[LiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData)the returned[State](https://developer.android.com/reference/kotlin/androidx/compose/runtime/State)will be updated causing recomposition of every[State.value](https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value())usage.

The[initial](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[initialized](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/index.html). Note that if[T](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[LifecycleOwner](https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)moves to the[Lifecycle.State.DESTROYED](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")
```