What is the deference between “remember” and “mutableState” in android jetpack compose? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of What is the deference between “remember” and “mutableState” in android jetpack compose? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m new in jetpack compose and trying to understand the deference between remember and mutableStateOf

In other words the deference between this line

val text = remember{ mutableStateOf("") }

and this

val text = remember{ "" }

and this also

val text = mutableStateOf("")


remember is a composable function that can be used to cache expensive operations. You can think of it as a cache which is local to your composable.

val state: Int = remember { 1 }

The state in the above code is immutable. If you want to change that state and also update the UI, you can use a MutableState. Compose observes any reads and writes to the MutableState object and triggers a recomposition to update the UI.

val state: MutableState<Int> = remember { mutableStateOf(1) }

   modifier = Modifier.clickable { state.value += 1 },
   text = "${state.value}",

Another variant (added in alpha12) called rememberSaveable which is similar to remember, but the stored value can survive process death or configuration changes.

val state: MutableState<Int> = rememberSaveable { mutableStateOf(1) }

Note: You can also use property delegates as a syntactic sugar to unwrap the MutableState.

var state: Int by remember { mutableStateOf(1) }

Regarding the last part of your question, if you are doing something like shown below within your composable, you are just creating a MutableState object.

val state: MutableState<Int> = mutableStateOf { 1 }

MutableState is an alternative to using LiveData or Flow. Compose does not observe any changes to this object by default and therefore no recomposition will happen. If you want the changes to be observed and the state to be cached use remember. If you don’t need the caching but only want to observe, you can use derivedStateOf. Here is a sample of how to use it.

We are here to answer your question about What is the deference between “remember” and “mutableState” in android jetpack compose? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji