What is best practice for sharing state / children...
# doodle
a
What is best practice for sharing state / children between Views? i.e., I have View A that has some functionality, and View B wants to access some data from View A. Would it be best to have all views be objects for this purpose?
n
I’d say it depends on several factors (i.e. is there a parent/child relationship between the Views, is the state dynamic/different between Views, etc.). But let’s assume both these things are true. Then it might make sense to have the parent View manage the lifecycle of the children, so they are created/added to it internally. This could happen as a result of some APIs on the parent. This gives you a lot of freedom to decide how the state is shared. You could pass a shared object to all such children for example. The situation is different if you can’t control the lifecycles. Then the question is whether the state is still tied to the View’s parent. If so, you can’t guarantee the child will have a parent of the type with the state since any View can be moved to any other one as a child. One option then would be to respond to
parentChanged
and either cast the new parent and fetch state it holds or use a known mapping to retrieve the state that is tied to the new parent.