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

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

My codes can be downloaded here →

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.