How can I force an update on the window? This is w...
# compose-desktop
d
How can I force an update on the window? This is what I'm trying to do: Press a specific letter or number and parts of the window changes (ex color) (When you press the letter/number it changes a variable) But it only updates when the application starts/window opens
a
Use
remember { mutableStateOf(…) }
to store a State in a composition.
d
I've tried to do this:
Copy code
val variable = remember { mutableStateOf(false) }
variable.value = !variable.value
but that didn't work 😕
Could it be because it's not in a @Composable function?
since I'm calling this on the key listener
m
The variable has to be in the @Composable function.
But you may (and it's usually the case) read and write to it in non-Composables
d
This is the code: https://paste.helpch.at/lomewinoqo.java So the variable is in a @Composable function, but the write to the variable is not, so maybe that's why it isn't working?
m
The code violates the rules of writing in Compose, so it can not work, although it indeed looks like it may happen to accidentally work. You also don't read the variable you change. You should really go though the tutorial for Compose, especially the chapter given above.
z
Yea reading through the tutorials should help. In general in cases like this, you need to hoist state used by multiple composables to a common parent (in this case, your
main
function). However, in the code sample you posted, it looks like
update
is a verb, and state is nouns not verbs. It sounds like you might want to store the color in a mutable state and then update that color when a key press happens. Then any composables reading that color will automatically update on the next frame.
344 Views