question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compose Web: wrong instance remembered when several remember calls in one scope

See original GitHub issue

Clicking the foo button the second and subsequent times causes the code for the bar button to be executed.

fun main() {
    renderComposableInBody {
        val words = remember { mutableStateListOf<String>() }
        words.map { P { Text(it) }}
        Button(attrs = { onClick { words.add("foo") } }) { Text("foo") }
        Button(attrs = { onClick { words.add("bar") } }) { Text("bar") }
    }
}

Following is the build.gradle:

plugins {
    kotlin("multiplatform") version "1.7.20"
    id("org.jetbrains.compose") version "latest.release"
    kotlin("plugin.serialization") version "latest.release"
}

repositories {
    mavenCentral()
    maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
    google()
}

kotlin {
    js(IR) {
        browser()
        binaries.executable()
    }
    sourceSets {
        val jsMain by getting {
            dependencies {
                implementation(compose.web.core)
                implementation(compose.runtime)
                implementation("io.ktor:ktor-client-core:latest.release")
                implementation("io.ktor:ktor-client-content-negotiation:latest.release")
                implementation("io.ktor:ktor-client-js:latest.release")
                implementation("io.ktor:ktor-serialization-kotlinx-json:latest.release")
            }
        }
    }
}

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
igordmncommented, Dec 6, 2022

as expected with compose version 1.2.0 and kotlin 1.7.10.

It probably should also work with Compose 1.2.1 and Kotlin 1.7.10, because Compose Multiplatform supports multiple Kotlin’s

1reaction
bgclcommented, Dec 6, 2022

Thank you @eymar for the quick diagnosis and a workaround that helped unblock me! I can’t wait to complete my 100% Kotlin (client+server) app and show it off to everybody 😃

PS: My specific case also worked fine using 1.2.1 and 1.7.10, but I’ll be more conservative and stick to 1.2.0 on 1.7.10.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Side-effects in Compose
A side-effect is a change to the state of the app that happens outside the scope of a composable function. Due to composables'...
Read more >
What does Jetpack Compose remember actually do, how ...
Compose offers tools for observing changes in your app's data, which will automatically recall your functions—this is called recomposing.
Read more >
Scoped recomposition in Jetpack Compose
When a recompose scope is invalidated, the compose runtime will ensure the (entire) function body gets recomposed (reexecuted) before the next ...
Read more >
4.4 Bean scopes
Scopes a single bean definition to a single object instance per Spring IoC ... scope (remembering that the singleton lifecycle scope is the...
Read more >
Variable scope, closure
If we call counter() multiple times, the count variable will be ... A closure is a function that remembers its outer variables and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found