Filip Wiesner
04/07/2021, 8:14 AMvar foo = remember { "hello" } , when we mutate the variable ( foo = "world" ) it won't save it to next recomposition, right? We just mutate the variable in the current function scope.Filip Wiesner
04/07/2021, 8:15 AMFilip Wiesner
04/07/2021, 8:16 AMwbertan
04/07/2021, 8:23 AMfoo.value = newValue and to access the value with foo.value.
To use the Delegate you have to use the `by`:
var foo by remember { mutableStateOf("hello") }
foo = "newValue"Filip Wiesner
04/07/2021, 8:25 AMState tho so there is no foo.value and no delegate.julioromano
04/07/2021, 8:26 AMfoo the way you mentioned, it will be difficult to ensure all composable functions that have foo visible in their scope see the same mutated value of foo.
If you need mutable state you should remember a mutableStateOf("hello").Filip Wiesner
04/07/2021, 8:31 AMremember and State separately because I don't want the reader to understand remember { mutableStateOf() } as one thing. While explaining this I realized that I am not 100% sure that I know how it will behave so I just wanted to make sure š
it will be difficult to ensure all composable functions that haveĀSo if some inner composable function recomposes, will it get the new value ofĀ visible in their scopefoo
foo variable?julioromano
04/07/2021, 8:40 AMSo if someĀ innerĀ composable function recomposes, will it get the new value ofĀĀ variable?foo
foo has been mutated.Albert Chang
04/07/2021, 8:56 AMremember always returns the same instance produced by executing the block in the first composition. foo is just a pointer so changing the value foo points to doesn't affect what remember returns.
So if someĀ innerĀ composable function recomposes, will it get the new value ofĀThe inner function captures the pointer so I think the answer is yes.Ā variable?foo
Filip Wiesner
04/07/2021, 8:59 AMFilip Wiesner
04/07/2021, 9:01 AMjulioromano
04/07/2021, 9:20 AMAlbert Chang
04/07/2021, 9:22 AMjulioromano
04/07/2021, 9:32 AMvar foo = remember { "hello" }
foo = "newValue"
It means that any other code will see ānewValueā, so in this case the remember call is useless, itās just polluting the composition by adding a āhelloā string to it that will never be read.
I get that @Filip Wiesner is asking this for teaching purposes, I just wanted to be sure such a āpatternā would never be actually useful in real life.Filip Wiesner
04/07/2021, 9:35 AMin this case theĀ(nitpicking a little bit) It will be read exactly once and assigned to the variable but other than that, that's exactly how I understand it.Ā call is useless, itās just polluting the composition by adding a āhelloā string to it that will never be readremember
julioromano
04/07/2021, 9:39 AMfoo briefly, just to be overwritten by ānewValueā right after.Filip Wiesner
04/07/2021, 9:40 AMjulioromano
04/07/2021, 10:36 AMButton) that recomposes on each click (recomposition is triggered thanks to someState snapshot state object):
@Composable
private fun MyOuterComposable() {
Column {
var searchValue = remember { "" }
var someState by remember { mutableStateOf(".") }
OutlinedTextField(
value = searchValue,
onValueChange = { searchValue = it },
)
Button({ someState = someState.plus(".") }) {
Text("someState: $someState - searchValue: $searchValue")
}
}
}
The value of searchValue that Button displays depends on when Button has been recomposed.
If it recomposes after searchValue has been mutated then it will show the mutated value, otherwise not.
Since the order in which composables (i.e. Button in this case) recompose may not always be reliably predicted, we canāt be sure which value of searchValue will be read by Button .
Disclaimer: this kind of code is just for learning purposes, it should never be used in production.Filip Wiesner
04/07/2021, 10:50 AMSean McQuillan [G]
04/07/2021, 4:33 PMSean McQuillan [G]
04/07/2021, 4:36 PMSean McQuillan [G]
04/07/2021, 4:37 PMReducer(...) or Prev(...) šjulioromano
04/07/2021, 4:40 PMReducer(...)Ā orĀ Prev(...) ?
Are they something in compose?
Or you mean Reducer as in Redux ?Sean McQuillan [G]
04/07/2021, 4:40 PM