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

Makes effects in composables more declarative #878

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Copy link
Contributor

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?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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

Copy link

Choose a reason for hiding this comment

The 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 LaunchedEffect instead so that it doesn't take one recomposition pass to apply currentOnTaskUpdate and then another to apply changes caused by that call.

val currentOnTaskUpdate by rememberUpdatedState(onTaskUpdate)
LaunchedEffect(Unit) {
currentOnTaskUpdate()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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.res.dimensionResource
import androidx.compose.ui.res.stringResource
Expand Down Expand Up @@ -92,9 +93,10 @@ fun TaskDetailScreen(
}

// Check if the task is deleted and call onDeleteTask
LaunchedEffect(uiState.isTaskDeleted) {
if (uiState.isTaskDeleted) {
onDeleteTask()
if (uiState.isTaskDeleted) {
val currentOnDeleteTask by rememberUpdatedState(onDeleteTask)
LaunchedEffect(Unit) {
currentOnDeleteTask()
}
}
}
Expand Down