Join Slack
Communities
Powered by
Hi guys, how can we access/instantiate a viewModel...
# compose
b
brabo-hi
07/31/2023, 5:46 PM
Hi guys, how can we access/instantiate a viewModel inside a compose @Preview function
s
Stylianos Gakis
07/31/2023, 6:49 PM
You shouldn't be working with viewmodels inside your previews. Read
https://developer.android.com/jetpack/compose/state-hoisting#screen-ui-state
particularly the section that says "Note: You shouldn't pass ViewModel instances down to other composables. For more information, see the Architecture state holders documentation." And that links to
https://developer.android.com/topic/architecture/ui-layer/stateholders#business-logic
✅ 4
a
alfredguimaz
07/31/2023, 6:58 PM
You want your composables to be testable so pull the viewmodel out so they dont have to depend on them
b
brabo-hi
07/31/2023, 7:17 PM
@Stylianos Gakis
@alfredguimaz
thanks for sharing, i think that’s the best approach
2
Views
Open in Slack
Previous
Next