Hello. I am having trouble with react state. So ba...
# react
e
Hello. I am having trouble with react state. So basically I am handling a list to show outside of component and updating the state via callback like this.
Copy code
val (state, setState) = useState(emptyList<Post>())

provider.observePosts { list -> setState(list) }
With this method the ui only updates once, however if I set state like this
setState(list.toList())
it works fine. I am concerning about performance. How should I update my list?
m
Don’t use a mutable list if you do - always pass a copy. Ideally at the point where the provider sends the update. It’s possible that that’s the reason for what you observe. If you always pass the same
MutableList
instance then the
state
will change from
emptyList()
to your
MutableList
and then always stays the same. I’m not sure if React compares old and new state and optimizes, but if it does it uses
===
.