Skip to content

Conversation

@leoMehlig
Copy link
Contributor

I noticed that any view using a @Default property wrapper, was getting their body re-evaluated twice when appearing.

This is what the logs look like when showing a simple view that uses @Default when adding a Self._printChanges().

DefaultView: @self, @identity, @8 changed.
DefaultView: @8 changed.

The first line is totally normal, but the second line indicates that the value of @Default changed (no idea why SwiftUI is referring to it as @8).

I noticed that the Observable the property wrapper is using, will always send a objectWillChange after being initialized, even though it is already initialized with the correct value. Adding a initial parameter fixes this and will still update the property wrapper (and view) when the value actually changes.

After the change, only the first line gets logged.

Here is also the test project I used for this: Host.zip

@sindresorhus sindresorhus changed the title Fix SwiftUI redraw Fix @Default triggering an extra redraw on appear Jan 5, 2025
@sindresorhus sindresorhus merged commit a6fc06a into sindresorhus:main Jan 5, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants