Unable to transition to the next screen modal with viewStore.binding in SwiftUI

We are developing with SwiftUI + the Composable Architecture.

I want to open a modal screen with sheet, but the screen does not transition with the following error.

whose view is not in the window hierarchy.

   This is working.

@State var isShowView = false  
〜〜
.sheet(isPresented: $isShowView) {
     ContentView()
}

This is not working.

.sheet(
   isPresented: viewStore.binding(
     get: \.isShowView,
     send: FormAction.showView
   )
) {
     ContentView()
}

What is the cause?

Answered by popoche in 693192022

@OOPer

The viewStore is here.

private let store: Store<FormState, FormAction>

WithViewStore(store) { viewStore in // ← viewStore
    ScrollView {
        VStack(spacing: 24) {
 〜〜〜
        }
     }
}

・FormState

public struct FormState: Equatable {

 public static func == (lhs: FormState, rhs: FormState) -> Bool { true }

 var isShowView = false
  
 public init() {
  }
 }
}

・FormAction

  case let .showCompleteView(isPresented):
   state.isShowCompleteView = isPresented
   state.eventComplete = isPresented ? .init(entryEvent: state.entryEvent) : nil
   return .none

I can confirm that sheet is called after going through FormAction, but I get the following error. whose view is not in the window hierarchy.

Can you show what is viewStore?

The viewStore is here.

private let store: Store<FormState, FormAction> WithViewStore(store) { viewStore in // ← viewStore     ScrollView {      VStack(spacing: 24) {  〜〜〜 } } }

・FormState

public struct FormState: Equatable {  public static func == (lhs: FormState, rhs: FormState) -> Bool { true }  var isShowView = false     public init() {   }  } }

・FormAction

  case let .showCompleteView(isPresented):    state.isShowCompleteView = isPresented    state.eventComplete = isPresented ? .init(entryEvent: state.entryEvent) : nil    return .none

I can confirm that sheet is called after going through FormAction, but I get the following error. whose view is not in the window hierarchy.

Accepted Answer

@OOPer

The viewStore is here.

private let store: Store<FormState, FormAction>

WithViewStore(store) { viewStore in // ← viewStore
    ScrollView {
        VStack(spacing: 24) {
 〜〜〜
        }
     }
}

・FormState

public struct FormState: Equatable {

 public static func == (lhs: FormState, rhs: FormState) -> Bool { true }

 var isShowView = false
  
 public init() {
  }
 }
}

・FormAction

  case let .showCompleteView(isPresented):
   state.isShowCompleteView = isPresented
   state.eventComplete = isPresented ? .init(entryEvent: state.entryEvent) : nil
   return .none

I can confirm that sheet is called after going through FormAction, but I get the following error. whose view is not in the window hierarchy.

Unable to transition to the next screen modal with viewStore.binding in SwiftUI
 
 
Q