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

Using @stable for the stateful composable wrapper is probably a bad idea #162

Open
atick-faisal opened this issue Feb 9, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@atick-faisal
Copy link
Owner

atick-faisal commented Feb 9, 2024

StatefulComposable.kt

In the context of the provided code, the use of the @Stable annotation likely doesn't make sense and could be removed. Here's why:

Purpose of @Stable

The @Stable annotation in Jetpack Compose has a specific function: It signals to the Compose compiler that a composable function has the following guarantees:

  1. The output only depends on its inputs: For the same set of input parameters, the function will always produce identical UI output.
  2. No side effects: The function won't modify any state that could affect recomposition behavior, beyond state parameters supplied to it.

Why it doesn't fit in this case

  • Side-effect potential: Your StatefulComposable potentially has a side effect with the triggering of the snackbar (onShowSnackbar) in the LaunchedEffect when an Error occurs. While the state itself might be stable, the snackbar introduces an element that modifies the UI beyond the basic composition defined by the function.
  • Statefulness: StatefulComposable likely already uses internal state mechanisms (remember value by, etc.) to manage changes to the underlying UiState. @Stable is usually more relevant to stateless composables.

When @Stable is Valuable

@Stable is a useful optimization hint for the Compose compiler. This annotation is best used with:

  • Smaller, stateless composable functions that are building blocks within your UI.
  • Functions where you want to guarantee zero recompositions unless any of the function's parameters change.

In conclusion, unless you have a very specific reason and can guarantee the absence of side effects, remove the @Stable annotation from your StatefulComposable.

@atick-faisal atick-faisal changed the title Using @stable for the stateful composable wrapper is probably a bad idea Using @stable for the [stateful composable wrapper](https://github.com/atick-faisal/Jetpack-Compose-Starter/blob/main/core/ui/src/main/kotlin/dev/atick/core/ui/utils/StatefulComposable.kt) is probably a bad idea Feb 9, 2024
@atick-faisal atick-faisal changed the title Using @stable for the [stateful composable wrapper](https://github.com/atick-faisal/Jetpack-Compose-Starter/blob/main/core/ui/src/main/kotlin/dev/atick/core/ui/utils/StatefulComposable.kt) is probably a bad idea Using @stable for the stateful composable wrapper is probably a bad idea Feb 9, 2024
@atick-faisal atick-faisal self-assigned this Feb 9, 2024
@atick-faisal atick-faisal added the bug Something isn't working label Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant