https://kotlinlang.org logo
#compose
Title
# compose
a

amar_1995

01/17/2020, 12:31 PM
I am using
+onCommit
and it is working perfectly fine. But I am unable to understand when
onDispose
is called.
Copy code
var user : User? = null
        // Active type use-case
        +onCommit(1) {
            user = UserAPI.getUserData()
            onDispose {
                UserAPI.letDiscompose() // It contains only println line
            }
        }
m

Manuel Wrage

01/17/2020, 12:33 PM
onDispose is called whenever the composable leaves the composition or the inputs have changed
a

amar_1995

01/17/2020, 12:40 PM
Is there any working example? I am still unable to figure it out.
z

Zsolt

01/17/2020, 1:03 PM
Composables methods that haven't changed are not re-run (recomposed) for efficiency. But once input values to the Composable method have changed, or stuff that you keep inside +state changed, or any other thing currently marked with the unary plus ("effect") has changed, then you really want to update to reflect new values, and invoke the Composable again (recompose). When that happens, you need to "clean up" the previous rendering, that's when onDispose gets called. (Also when the Composable is not updated but removed for good). In my understanding onCommit gets called on every recomposition, while onActive only gets called on the first one (please correct me if I'm wrong).
a

amar_1995

01/17/2020, 1:35 PM
onActive is similar to onCommit when
+onCommit(anyConst)
or
+onCommit(true)
is used. I read somewhere onActive will be removed in future.
👍 1
I am this error while using onCommit
java.lang.IllegalStateException: no stage finishing is allowed while read is disabled
Any one know why ?