-
Notifications
You must be signed in to change notification settings - Fork 11.7k
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
Makes effects in composables more declarative #878
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ import androidx.compose.material.rememberScaffoldState | |
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.res.dimensionResource | ||
|
@@ -84,9 +85,10 @@ fun AddEditTaskScreen( | |
) | ||
|
||
// Check if the task is saved and call onTaskUpdate event | ||
LaunchedEffect(uiState.isTaskSaved) { | ||
if (uiState.isTaskSaved) { | ||
onTaskUpdate() | ||
if (uiState.isTaskSaved) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is using recomposition as an observer of state change events to apply other changes to app state. Avoid this. Observe the state changes inside the |
||
val currentOnTaskUpdate by rememberUpdatedState(onTaskUpdate) | ||
LaunchedEffect(Unit) { | ||
currentOnTaskUpdate() | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this run on every recomposition, regardless of if
uiState.isTaskSaved
changed or not?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is answered here: https://stackoverflow.com/questions/69085027/difference-between-remember-and-rememberupdatedstate-in-jetpack-compose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. This if statement will be check on every recomposition