Home > Software engineering >  Should I to wrap variables with remember as much as possible for @Composable function when I use Jet
Should I to wrap variables with remember as much as possible for @Composable function when I use Jet

Time:07-28

I know remember can reduce recomposition in @Composable function, but sometimes I forget to add remember for simple variables.

Should I to wrap variables with remember as much as possible for @Composable function when I use Jetpack Compose ? Just Like Code A should be Code B?

Code A

@Composable
fun DialogForDBWarningValue(
    preferenceState:PreferenceState
) {
    val context = LocalContext.current

    val itemName = stringArrayResource(R.array.dBWarning_Name)
    val itemValue = stringArrayResource(R.array.dBWarning_Values)

    val temp = loadDBWarningValueWithPreference(context)
    preferenceState.iniSelectedItemValue = temp

    val iniSelectedItemName = itemName[itemValue.indexOf(preferenceState.iniSelectedItemValue)]
 
}

Code B

@Composable
fun DialogForDBWarningValue(
    preferenceState:PreferenceState
) {
    val context = LocalContext.current  //It can't be wrapped with remember

    val itemName = stringArrayResource(R.array.dBWarning_Name)     //It can't be wrapped with remember
    val itemValue = stringArrayResource(R.array.dBWarning_Values)  //It can't be wrapped with remember

    val temp =  remember {
        loadDBWarningValueWithPreference(context)
    } 
    
    preferenceState.iniSelectedItemValue = temp

    val iniSelectedItemName = remember {
        itemName[itemValue.indexOf(preferenceState.iniSelectedItemValue)]
    }


}

CodePudding user response:

remember doesn't reduce recompositions, it makes sure that your calculation inside remember survives recomposition. And you can reset and redo calculation by setting keys for remember function.

Remember the value produced by [calculation]. [calculation] will only be evaluated during the composition. Recomposition will always return the value produced by composition.

You should use remember when you don't want an object to be instantiated again on recomposition with initial values of that object.

class MyObject(var counter:Int =0)

if you don't wrap instantiation inside remember on every recomposition you will have a new object with counter = 0

Using remember for creating Animatable might be another example. Compiler returns warning when you don't use remember

val animatable =  Animatable(0f)

Creating an Animatable during composition without using remember

Another example is when you need to do some heavy work that doesn't need to be done in every recomposition, keys you set play a pivotal role because in some cases you want to do a new calculation with new value. For instance creating a new Bitmap is needed when user picks a new Image or property like content scale changes.

val scaledBitmap = remember(imageBitmap, contentScale, aspectRatio){ 
      // some heavy work is done here
}

Another common usage of remember is in Layout functions to remember measure and layout calculation(MeasurePolicy) unless some properties change. For instance BoxWithConstraints

@Composable
@UiComposable
fun BoxWithConstraints(
    modifier: Modifier = Modifier,
    contentAlignment: Alignment = Alignment.TopStart,
    propagateMinConstraints: Boolean = false,
    content:
        @Composable @UiComposable BoxWithConstraintsScope.() -> Unit
) {
    //            
  • Related