Referring to parent state #1445
Replies: 2 comments 9 replies
-
I'm not that experienced, but would passing through a non-scoped store besides the scoped one solve the problem? |
Beta Was this translation helpful? Give feedback.
-
So I've got a few code examples this time Here's a basic view of the user dependency I was talking about:
Here, test and test2 don't return the same thing, as they should with a single copy of User state. There's two solutions I came up with: a manual injection method, which is really verbose and would probably need codegen in order to scale
And a reference method that breaks our nice copy semantics, but is vastly less code and is much easier to read without codegen.
Checking on godbolt shows them to be similar generated code sizes with the compiler able to infer the result of the explicit struct implementation, albeit this is a toy problem so who knows about more complex problems. Please let me know what you think! |
Beta Was this translation helpful? Give feedback.
-
Suppose we have an app with a User struct with the following view / reducer hierarchy.
A -> B -> ... -> Z
A provides the user, B - Y doesn't need the user state, and Z needs the user state. To get the state from A to Z without losing synchronization, only a few options seem possible:
objectWillChange
async sequence for Z to update its user off of. This seems like the most promising approach but I'd like some confirmation that dependencies (among other things) would work in such a configuration - I'm new to this architecture and there could just be something that I'm missing.At its core, the problem is a composable child referring to the state of its parent, something hidden by the 'one giant state' redux solutions. I was kicking around the idea of a rust-style
Cow
construct that could be passed in, where an independent view could use an owned User state (for instance, SwiftUI previews) while a child view could use a borrowed User state, and this state could be passed through the view hierarchy implicitly somehow, but I could only find the above as actually practical solutions.If anyone has any idea how to make globally visible state, let me know!
Beta Was this translation helpful? Give feedback.
All reactions