I have a view that is getting large and I want to factor out a chunk of it. One option is to extract a subview by creating a new struct that inherits from View
and defines the body
property. Another option is to create a function with a return type of some View
. It's a bit less code that way. But I get the impression that creating a new View
is preferred. What advantages does that have?
struct DemoView: View {
var body: some View {
Text("Demo")
}
}
vs.
func demoView() -> some View {
Text("Demo")
}
It does not matter that much, but the common practice is more to use struct.
However, func make it a little easier to pass parameter, as they are declared in parameters list instead of a var in the struct. But that's real minimal difference.
As far as I've seen, it is thus more a convenience choice.