Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composable fun koinInject() does not recreate the entity with new parameters #1965

Open
puritanin opened this issue Sep 3, 2024 · 2 comments

Comments

@puritanin
Copy link

Describe the bug
Composable fun koinInject() does not recreate the entity with new parameters in last versions of Koin (3.5.3, 4.0.0-RC2).

Old version of deprecated fun get() - solves the problem (but with nuances):

@Composable
@Deprecated("use koinInject() instead")
inline fun <reified T> get(
    qualifier: Qualifier? = null,
    scope: Scope = currentKoinScope(),
    noinline parameters: ParametersDefinition? = null,
): T = remember(qualifier, parameters, scope) {               <=== HERE (recreate)
    scope.get(qualifier, parameters)
}

Version 3.5.3 - does not solve the problem:

@Composable
inline fun <reified T> koinInject(
    qualifier: Qualifier? = null,
    scope: Scope = currentKoinScope(),
    noinline parameters: ParametersDefinition? = null,
): T {
    val st = parameters?.let { rememberStableParametersDefinition(parameters) }
    return remember(qualifier, scope) {                                            <=== HERE (does not recreate)
        scope.get(qualifier, st?.parametersDefinition)
    }
}

Version 4.0.0 - does not solve the problem:

@Composable
inline fun <reified T> koinInject(
    qualifier: Qualifier? = null,
    scope: Scope = currentKoinScope(),
    noinline parameters: ParametersDefinition? = null,
): T {
    // This will always refer to the latest parameters
    val currentParameters by rememberUpdatedState(parameters)

    return remember(qualifier, scope) {                                     <=== HERE (does not recreate)
        scope.get(qualifier) {
            currentParameters?.invoke() ?: emptyParametersHolder()
        }
    }
}

Expected behavior
The fun koinInject() should recreate the entity when the parameters change, like the previous fun get().

Koin module and version:
koin-compose
3.5.3 - 4.0.0-RC2

Snippet or Sample project to help reproduce

https://gist.github.com/puritanin/e23d9948f1909d2dbbb8d34675969e95

@fabian-kotschenreuther
Copy link

We have the same issues. Any update here?

@lacky1991
Copy link

I’d like to emphasize the impact of this issue on my project, as it currently prevents me from re-creating instances with new parameters in Compose, which is essential for my use case. The previous get function behavior met this need, and its absence limits Koin's utility in parameter-dependent scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants