Skip to content

Document best practices for shared stores #61

@bbrk24

Description

@bbrk24

Checklist

Request

When sharing an @Store object from a view to its subview, this is legal with an implicit initializer:

struct Superview: View {
    @Store var object: StoreType

    var body: some View {
        Subview(object: self.object)
    }
}

struct Subview: View {
    @Store var object: StoreType

    var body: some View {
        // ...
    }
}

but it's not clear from the documentation whether this is good practice or not. Furthermore, this becomes more complicated with an explicit initializer, since Store.wrappedValue is get-only:

struct Superview: View {
    @Store var object: StoreType

    var body: some View {
        Subview(object: self._object)
    }
}

struct Subview: View {
    @Store var object: StoreType

    init(object: Store<StoreType>) {
        self._object = object

        // then, property access must be done as either
        self.object.someThing
        // or
        object.wrappedValue.someThing
    }

    var body: some View {
        // ...
    }
}

In some cases, it may not be necessary for them to both be @Store, but this prevents both views from accessing the projected value $object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions