In the thread code, why I get a warning about retu...
# compose
p
In the thread code, why I get a warning about returning a State without using remember?
Copy code
Creating a state object during composition without using remember
Copy code
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
class AndroidWindowSizeInfoProvider(
    val activity: Activity,
): IWindowSizeInfoProvider {

    //@SuppressLint("UnrememberedMutableState")
    @Composable
    override fun windowSizeInfo(): State<WindowSizeInfo> {
        val windowSize = calculateWindowSizeClass(activity)

        return derivedStateOf {
                when(windowSize.widthSizeClass){
                    WindowWidthSizeClass.Compact -> WindowSizeInfo.Compact
                    WindowWidthSizeClass.Medium -> WindowSizeInfo.Medium
                    WindowWidthSizeClass.Expanded -> WindowSizeInfo.Expanded
                    else -> throw IllegalStateException()
                }

        }

    }

}
Above code makes lint warning me about using derivedStateOf without wrapping it in a remember block. Why is this? - Should I still remember it if I am returning it from a function? I plan to remember it in the caller composable function scope not in this producer scope
d
If you plan to remember it in the caller then
windowSizeInfo()
probably shouldn't be marked composable.
p
Humm, I annotated @Composable because
calculateWindowSizeClass(activity)
is Composable, and the library doesn’t provide a non composy api. They:
implementation( "androidx.compose.material3:material3-window-size-class:1.0.1"
But well, I might end up putting a remember on it
Oh wait, I seem to find a non @Composy way of calling their function, thanks fellows