title: SwiftUI Modifying state during view update, this will cause undefined behavior - Proper use explanation
body: <p>Im learning swift and this error/warning is driving me crazy because I cant see what call Im making that causing it... The Xcode warning only shows up in my @main struct</p>
  <p><strong>Modifying state during view update, this will cause undefined behavior.</strong></p>
  <p>I thought it might be in the ListView, but I realized the warning only shows after the &quot;Submit Post&quot; button is it.</p>
  <p>Im looking for a fix, but more importantly and explanation as to why this is happening and the proper usage moving forward.</p>
  <pre><code>import SwiftUI
  import Firebase
  
  @main
  
  struct SocialcademyApp: App {
      
      init() {
          FirebaseApp.configure()
      }
      
      var body: some Scene {
          WindowGroup {
              PostsList()
          }
      }
  }
  </code></pre>
  <pre><code>struct PostsList: View {
      
     @StateObject var viewModel = PostsViewModel()
     @State private var searchText = &quot;&quot;
     @State private var showNewPostForm = false
      
      var body: some View {
          
          NavigationView {
              List(viewModel.posts) { post in
                  if searchText.isEmpty || post.contains(searchText) {
                      PostRow(post: post)
                  }
              }
              .searchable(text: $searchText)
              .navigationTitle(&quot;Posts&quot;)
              .toolbar {
                  Button {
                      showNewPostForm = true
                  } label: {
                      Label(&quot;New Post&quot;, systemImage: &quot;square.and.pencil&quot;)
                  }
              }
              .sheet(isPresented: $showNewPostForm) {
                  NewPostView(creationAction: viewModel.makeCreationAction())
              }
          }
          
      }
  }
  </code></pre>
  <pre><code>struct NewPostView: View {
      
      typealias CreationAction = (Post) async throws -&gt; Void
      let creationAction: CreationAction
      
      @State private var post = Post(title: &quot;&quot;, content: &quot;&quot;, authorName: &quot;&quot;)
      @State private var state = FormState.idle
      
      @Environment(\.dismiss) private var dismiss
      
      var body: some View {
          NavigationView {
              Form {
                  Section {
                      TextField(&quot;Title&quot;, text: $post.title)
                      TextField(&quot;Author Name&quot;, text: $post.authorName)
                  }
                  Section {
                      TextField(&quot;Content&quot;, text: $post.content)
                          .multilineTextAlignment(.leading)
                  }
                  Button(action: createPost, label: {
                      if state == .working {
                          ProgressView() } else {
                              Text(&quot;Submit Post&quot;)
                          }
                  })
                  .font(.headline)
                  .frame(maxWidth: .infinity)
                  .foregroundColor(.white)
                  .padding()
                  .listRowBackground(Color.accentColor)
              }
          }
          .navigationTitle(&quot;New Post&quot;)
          .disabled(state == .working)
          .alert(&quot;Cannot Create Post&quot;, isPresented: $state.isError, actions: {}) {
              Text(&quot;Sorry, something went wrong&quot;)
          }
          .onSubmit {
              createPost()
          }
      }
      
      private func createPost() {
          print(&quot;[NewPostForm] creating a new post&quot;)
          Task {
              state = .working
              do {
                  try await creationAction(post)
                  dismiss()
              } catch {
                  state = .error
                  print(&quot;[NewPostForm] Cannot create post: \(error)&quot;)
              }
          }
      }
  }
  
  private extension NewPostView {
      
      enum FormState {
          case idle, working, error
          
          var isError: Bool {
              get {
                  self == .error
              }
              set {
                  guard !newValue else { return }
                  self = .idle
              }
          }
      }
      
      
      
  }
  </code></pre>
  <pre><code>@MainActor
  class PostsViewModel: ObservableObject {
      @Published var posts = [Post.testPost]
      
      func makeCreationAction() -&gt; NewPostView.CreationAction {
          return { [weak self] post in
              try await PostsRepository.create(post)
                  self?.posts.insert(post, at: 0)
          }
      }
  }
  </code></pre>