Module 4 Lesson 15 Challenge - Button for fav not working

Please help. I really don’t any idea what’s going on. I even copied the implementation from the solution because all the sensible logic really didn’t work for me. It’s in the DetailView and the snippet looks like this

 NavigationLink(
                    destination: PageView(model:book)){
                   
                        Image(book.cover!)
                            .resizable()
                            .scaledToFit()
                            .padding(.horizontal, 40)
                    };
                
                Text("Mark for later!")
                    .font(.headline).padding([.top, .leading, .trailing])
                
                Button(action: { viewModel.updateFavourite(forId: book.id) }) {
                    Image(systemName: book.isFavourite ? "star.fill" : "star")
                        .resizable()
                        .frame(width: 28, height: 28)
                }
                .accentColor(.yellow)
                

My codes can be downloaded here →
Archive.zip

I resolved this by using bindings in all the sub views. Then I use Toggle bound to Book.isFavourite property. The solution uses Button which I thought is awkward.

Then from the parent view, the ListView, I passed the book as a bound objectm $book

This also eliminates the extraneous codes to transfer the states like that in the .onAppear(…) modifer of the the Staging view.

1 Like

There is more than one way to solve a challenge. Whatever works for you is fine.