Hello everybody! Currently testing around in a CM...
# multiplatform
c
Hello everybody! Currently testing around in a CMP project. Im having trouble updating the state inside a swiftUI view.
Copy code
@Composable
actual fun NativeButton(
    enabled: Boolean,
    onClick: () -> Unit,
    modifier: Modifier
) {
    val factory: NativeViewFactory = LocalNativeViewFactory.current

    UIKitViewController(
        modifier = modifier,
        factory = {
            factory.createNativeButton(
                enabled = enabled,
                onClick = onClick,
            )
        }
    )
}
Changing the enabled flag is not updating the UI in SwiftUI. How do i achieve this? Any suggestions?
t
Add an
update
callback.
factory
only gets called on the first composition. Subsequent recompositions need to pass any change in state through an
update
callback. Annoying that I couldn’t find it in the docs, but here’s the signature in source: https://github.com/JetBrains/compose-multiplatform-core/blob/86802de2395e82f726fce[…]uikitMain/kotlin/androidx/compose/ui/interop/UIKitView.uikit.kt
c
Thanks! Managed to solve it with something like this:
Copy code
@Composable
actual fun NativeButton(
    enabled: Boolean,
    onClick: () -> Unit,
    modifier: Modifier
) {
    val factory: NativeViewFactory = LocalNativeViewFactory.current
    val enabledState = remember { MutableStateFlow(enabled) }

    UIKitViewController(
        modifier = modifier,
        factory = {
            factory.createNativeButton(
                enabled = enabledState.asStateFlow(),
                onClick = onClick,
            )
        },
        update = {
            enabledState.update { enabled }
        }
    )
}
SO to @Richard that responded in another 🧵.