diff --git a/topics/compose/compose-multiplatform-resources-usage.md b/topics/compose/compose-multiplatform-resources-usage.md index b89df540..5b1bcce5 100644 --- a/topics/compose/compose-multiplatform-resources-usage.md +++ b/topics/compose/compose-multiplatform-resources-usage.md @@ -101,18 +101,37 @@ To get string resources as a `String`, use the following code: ```kotlin @Composable -fun stringResource(resource: StringResource): String {...} +fun stringResource( + resource: StringResource, + default: String = "", +): State = + produceState(initialValue = default) { + value = getString(resource = resource) + } @Composable -fun stringResource(resource: StringResource, vararg formatArgs: Any): String {...} +fun stringResource( + resource: StringResource, + vararg formatArgs: Any, + default: String = "", +): State = + produceState(initialValue = default) { + value = getString( + resource = resource, + formatArgs = formatArgs, + ) + } ``` For example: ```kotlin -Text(stringResource(Res.string.app_name)) +var appName by stringResource(Res.string.app_name) +Text(text = appName) ``` +This example uses `produceState` within `stringResource` to load the string from resources. The `appName` variable is then updated on recomposition. +