Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaslogen authored Nov 25, 2023
1 parent 61d8aa2 commit e5db8fb
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions resacahilt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ visible for the first time. The ViewModel will cleared and destroyed when the it
When using the Lazy* family of Composables it is recommended that -just above the call to the Lazy* Composable- you use `rememberKeysInScope` with a list of
keys corresponding to the items used in the Lazy* Composable to obtain a `KeyInScopeResolver` (it's already highly recommended in Compose that items in a Lazy* list have unique keys).

Then in the Lazy* Composable, once you are creating an item and you need an object or ViewModel for that item,
all you have to do is include in the call to `rememberScoped`/`viewModelScoped` the key for the current item and the `KeyInScopeResolver` you previously got from `rememberKeysInScope`.
Then in the Lazy* Composable, once you are creating an item and you need a ViewModel for that item,
all you have to do is include in the call to `hiltViewModelScoped` the key for the current item and the `KeyInScopeResolver` you previously got from `rememberKeysInScope`.

With this setup, when an item of the Lazy* list becomes visible for the first time, its associated `rememberScoped`/`viewModelScoped` object will be created and even if the item is scrolled away, the scoped object will still be alive. Only once the associated key is not present anymore in the list provided to `rememberKeysInScope` and the item is either not part of the Lazy* list or scrolled away, then the associated object will be cleared and destroyed.
With this setup, when an item of the Lazy* list becomes visible for the first time, its associated `hiltViewModelScoped` ViewModel will be created and even if the item is scrolled away, the scoped ViewModel will still be alive. Only once the associated key is not present anymore in the list provided to `rememberKeysInScope` and the item is either not part of the Lazy* list or scrolled away, then the associated ViewModel will be cleared and destroyed.

<details>
<summary>Example of a different ViewModel for each item in a LazyColumn and scope them to the Composable that contains the LazyColumn</summary>
Expand Down

0 comments on commit e5db8fb

Please sign in to comment.