-1

Normally in SwiftUI, data which is owned by the view and used to render it is held in a private property marked with the @State property wrapper. This tells the system to track changes to that property so it knows when to update the view.

But... if that data never changes, and thus is marked with let and is set via the initializer, do you still need the @State attribute?

Additionally, since WidgetKit's views are all immutable by design (let alone ran in a completely separate/isolated process owned by the system), is there ever a case to use any of the state-related modifiers for views exclusively used in Widgets?

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286

1 Answers1

0

If your property never changes, just declare it without @State and use 'let'. You can pass its value at init time as an argument when you create your View.

  • Yeah, that's what we're currently doing, but we're getting layout issues in our widgets. Specifically we have an HStack that shows three items, determined by the backend. As such, the same item could appear again, but in a different position. Additionally, some items show one line of text while others show two, thanks to wrapping and the length of that text, and it's bottom-aligned. What we're seeing is the text isn't properly updating, and thus the one-line/two-line thing isn't properly laying itself out causing layout issues. – Mark A. Donohoe Sep 07 '21 at 16:12