Reputation: 2670
I am facing an issue in refreshing a list based on a state change.
So I have a state variable which is using to show/hide a sheet , and while showing the sheet some data will be updated and the updated data needs to be shown on the list .This is my expectation , but the issue is that the list is not updating based on state change.As per my understanding , the entire View will be re-rendered when any state variable update happens, if so could anyone please check the below code and let me know why the list is not updating after the self.showSheet.toggle()
inside the ProcessDocument
closure:
struct DetailView: View {
@Binding var document: Doc
@State private var showSheet: Bool = false
var body: some View {
List {
Section(header: smallHeaderText("DETAILS")) {
DocTypesPicker(fleetId: fleet?.fleetId ?? "", docTypeId: $document.doc_type)
}
Section(header: smallHeaderText("IMAGES")) {
NavigationLink(destination: DetailImagesPage(document: document) ) {
HStack {
let docImage = self. document.documentData?.first?.img ?? UIImage().jpegData(compressionQuality: 1.0)
Image(uiImage: UIImage(data: docImage!) ?? UIImage())
.resizable()
.frame(width: 60, height: 60, alignment: .center)
VStack(alignment: .leading) {
Spacer()
Text(self.pageCountFor(document: self.document))
Spacer()
}.padding(.leading, 14)
}
}.listRowBackground(Color.white)
}
Section {
HStack {
Spacer()
Button("Redo") {
self.showSheet.toggle()
}
Spacer()
}
}
}
.listStyle(GroupedListStyle())
.sheet(isPresented: $showSheet) {
ProcessDocument { docs in
guard let docs = docs, let currentDoc = document.documentData else { return }
document.removeFromDocumentData(currentDoc as NSSet)
docs.forEach { doc in
let dateCreated = document.date_created
doc.doc_id = dateCreated
document.addToDocumentData(doc)
}
self.showSheet.toggle()
}
}
}
}
Upvotes: 4
Views: 1222
Reputation: 258247
As per my understanding , the entire View will be re-rendered when any state variable update happens
Not this is not that way, updated only parts of view's body (subviews) explicitly dependent on that state (ie. if state inside Text
- only that one Text
will be updated, even if you have 10 more other views in body).
In your case there is no other dependency on showSheet
inside body except sheet itself, so nothing, including List
, is updated.
If you want to refresh entire List
explicitly on showSheet
the possible solution could be as follows
List {
// ... content here
}
.id(showSheet) // << here !!
Upvotes: 2