When using @State to modify a singleton instance, the UI cannot be refreshed.
Read the original article:When using @State to modify a singleton instance, the UI cannot be refreshed Context When a singleton class instance is decorated with @State, changing its internal proper...

Source: DEV Community
Read the original article:When using @State to modify a singleton instance, the UI cannot be refreshed Context When a singleton class instance is decorated with @State, changing its internal properties does not trigger a UI refresh. For example: in Page2, after toggling the color and returning to the Index page, the UI does not update accordingly. Description @State allows UI to refresh when the state variable itself changes. However, it does not track property changes inside a class instance. To observe inner property changes, @Observed is needed. Alternatively, AppStorage can provide application-level state sharing, and variables with @StorageLink will synchronize automatically. Solution / Approach Option 1: Use @Observed Decorate the singleton class with @Observed. This makes @State capable of observing property changes within the instance. @Observed export class VoiceReadHelper { private static instance: VoiceReadHelper | null = null isSpeaking = false private constructor() { } sta