I need to hoist a string variable in on a onClick ...
# compose
p
I need to hoist a string variable in on a onClick event:
Copy code
onClick = { onMessageDialogEvent(stringResource(Res.string.bus_stops_db_filled, successUiState.data.size)) },
But stringResource can't be called because onClick is not a composable:
Copy code
@Composable invocations can only happen from the context of a @Composable function
How can this situation be solved? I need to transform that resource into a string before hoisting it, because it is stored on a app state holder which requires it on string, for some complex stuff (a lot of string resources requires a lot of parameters, and I can't hoist all those multiple parameters, it's easy to simply hoist the string)
c
Copy code
val str = stringResource(Res.string.bus_stops_db_filled)  Button(onClick = { onMessage(str) })
☝️ 1
☝🏻 1
p
is that approach recommended?
if you hace 10 calls you need to have 10 variables preloaded
if you have 100, 100, etc...
c
You won't have 100 button.
if so, you should make them into array
z
Another option, only on android though (although probably something similar is possible on other platforms), is read
LocalContext
in composition instead then pull the string resource out of the context’s resources in the callback.
s
Compose Components Resources has
suspend fun getString(res: StringRes): String
function that is suitable for non composable function.