Reputation: 5075
I have this function to return a reusable view when my side menu is clicked. When the function is called for the first time GameMenuView is initialized, but the next time it's called, the view is already initialized and I would like to reinitialize it since I want it to behave like a different view with onAppear being called and so on.
Any idea how to accomplish this?
func getGameMenuView(isGameOne: Bool, menuInteractor: MenuInteractor) -> some View {
let gameInteractor = GameInteractor(isGameOne: isGameOne)
return AnyView(
GameMenuView()
.environmentObject(menuInteractor)
.environmentObject(gameInteractor)
)
}
Upvotes: 0
Views: 59
Reputation: 257663
SwiftUI views are value types and provided function creates/initialized a new view, so I believe your issue is not in provided code but in code which uses this function.
Anyway try to give it unique ID (or in places where you use result of this function), this prevents SwiftUI equality optimization.
func getGameMenuView(isGameOne: Bool, menuInteractor: MenuInteractor) -> some View {
let gameInteractor = GameInteractor(isGameOne: isGameOne)
return AnyView(
GameMenuView().id(UUID()) // << here !!
.environmentObject(menuInteractor)
.environmentObject(gameInteractor)
)
}
Upvotes: 1