https://kotlinlang.org logo
Title
j

jasu

03/12/2022, 7:56 AM
I’ve compose hierarchy like below, que is NoteListComponent is querying db unnecessarily when I goto detail and come back (because of launchedEffect) is there any way I can ignore unwanted db queries ?
NoteListComponent(
notes: List<Note>
…coupleOfParameters
) {
    var searchText by rememberSaveable { mutableStateOf("") }

    LaunchedEffect(key1 = searchText) {
        notesViewModel.searchNotes(searchText)
    }
    LazyList(...withParams)

}
👀 1
a

Arsen

03/12/2022, 1:23 PM
Use state hoisting to VM. Keep in state: • notes List<> • searchText String • searchTextOfNotes String? // updates when notes loaded successfully In searchNotes event check searchText on equality with searchTextOfNotes to avoid redundant requests. Btw. with full state hoisting you don't need LaunchedEffect at all, just do logic inside VM's init block
j

jasu

03/12/2022, 1:26 PM
yeh make sense
thanks I’ll try to implement this way