How can I force to recompose LazyColumn when the l...
# compose
a
How can I force to recompose LazyColumn when the list does not update even if the list is changed?
f
If you have to force a recomposition of any component, it's most likely bad state management on your side. Can you post a snippet of your state (List) and how you update it?
☝️ 2
a
Give me a second I try to put something together for you
Copy code
@Composable
fun PlacesSearch(
    currentlySearchedPlaces: List<PlaceEntity>,
    onItemBookmarkClicked: (PlaceEntity) -> Unit,
    onPlaceSearched: (PlaceEntity) -> Unit,
    onPlaceClicked: (PlaceEntity) -> Unit,
) {
    if (currentlySearchedPlaces.isNotEmpty()) {
        Log.e("Searched places", "$currentlySearchedPlaces")

        RulonaSearchList(
            places = currentlySearchedPlaces,
            onItemClick = { uuid ->
                onPlaceClicked(uuid)
                onPlaceSearched(uuid)
            },
            onItemBookmarkClicked = {
                onItemBookmarkClicked(it)
            },
        )
    }
}
@Composable
fun RulonaSearchList(
    places: List<PlaceEntity>,
    onItemClick: (PlaceEntity) -> Unit,
    onItemBookmarkClicked: (PlaceEntity) -> Unit,
) {
    Log.e("rendering places", "$places")

    LazyColumn {
        items(places) { place ->
            Log.e("item in list", "$place")

            RulonaSearchItem(
                title = place.name,
                isBookmarked = place.isBookmarked,
                onClick = { onItemClick(place) },
                onBookmarkClicked = { onItemBookmarkClicked(place) },
            )
        }
    }
}
As you can see in the logs, the items correctly update with the new bookmarked state but the "item in list" log is not called again, which means that the SearchItem does not recompose inside the list
f
And where do you hold the
currentlySearchedPlaces
? It looks that you have that data in a VM somewhere. Do you use
collextAsState()
on your search places?
a
I have something like this:
Copy code
val placesUiState = viewModel.collectAsState(PlacesState::uiState)

PlacesSearch(
    currentlySearchedPlaces = uiState.currentlySearchedPlaces,
    onItemBookmarkClicked = { place ->
        viewModel.action(TogglePlaceBookmarkUseCase(place))
    },
    onPlaceSearched = { place ->
        viewModel.action(SetPlaceSearchTimestampUseCase(place))
    },
    onPlaceClicked = { place -> onPlaceClicked(place.uuid) },
)
a
a
the thing is, I already tried to create a brand new list with toMutableList() or .map {it.copy() } and it did not recompose either
a
if you change from
items(places) {…}
to
items(places, key = { it.toString() } ) {}
will it work?
a
yes, now it works 😅 thank you very much @Andrey Kulikov
i tried to use key before but the signature was not showing it since items() is overloaded
a
just to clarify: it is not really a correct way of doing it and your code should work without such hack. but it helps to workaround the bug introduced in beta08 which I am trying to fix atm. once it is fixed you should consider passing your uuid as a key, not toString()
a
yea it worked before i updated to beta 08 i am sure, will add a comment