# How does SwiftUI find @State properties in views?

**URL:** <https://forums.swift.org/t/how-does-swiftui-find-state-properties-in-views/79984>\
**Category:** Using Swift\
**Tags:** off-topic, swiftui\
**Created:** [May 19, 2025, 2:42pm UTC](https://forums.swift.org/t/how-does-swiftui-find-state-properties-in-views/79984 "2025-05-19T14:42:23Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [May 27, 2025, 4:21am UTC](https://forums.swift.org/t/how-does-swiftui-find-state-properties-in-views/79984/22 "2025-05-27T04:21:35Z")

</div>

For `View.onChange`: refer to [OpenAsyncImage/Sources/OpenAsyncImage/onChange+Backport.swift at 31aa0d53b0509f3670c17ab13130b80a99f1780d · OpenSwiftUIProject/OpenAsyncImage · GitHub](https://github.com/OpenSwiftUIProject/OpenAsyncImage/blob/31aa0d53b0509f3670c17ab13130b80a99f1780d/Sources/OpenAsyncImage/onChange%2BBackport.swift#L109-L136)

For `State`: refer OpenSwiftUIProject/OpenSwiftUI's State implementation which use Location system based implementation.

---

<div class="post-metadata">

**Author:** ![farzadshbfn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/farzadshbfn/32/6592_2.png) [@farzadshbfn](https://forums.swift.org/u/farzadshbfn)\
**Post date:** [May 27, 2025, 8:47am UTC](https://forums.swift.org/t/how-does-swiftui-find-state-properties-in-views/79984/23 "2025-05-27T08:47:57Z")

</div>

Thanks for the explanation.

It is very unintuitive (and genius at the same time) how it works! I was looking at it imperatively, and was not considering that `onChange` is a ViewModifier and I should think declarative.

So the only reason an API like `onChange` is working, is because when `@State` changes, view re-renders, `body` is called again, hence this `onChange` method is called with the latest **L-Value** of `View'`s `@State`. Which now the ViewModifier can compare it with its local storage.

---

<div class="post-metadata">

**Author:** ![BrentM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/brentm/32/15134_2.png) [@BrentM](https://forums.swift.org/u/BrentM)\
**Post date:** [May 28, 2025, 4:25pm UTC](https://forums.swift.org/t/how-does-swiftui-find-state-properties-in-views/79984/24 "2025-05-28T16:25:17Z")

</div>

Im not sure about state, but observable object's @Published properties use a hidden feature of property wrappers, which allows you to access the parent type. I imagine @State might leverage this somehow?

I use it here to trigger objectWillChange: [AsyncValue/Sources/AsyncValue/AsyncValue.swift at c04bdc325ad7f48dd2eb43bdb401c6cfaa5019e3 · BrentMifsud/AsyncValue · GitHub](https://github.com/BrentMifsud/AsyncValue/blob/c04bdc325ad7f48dd2eb43bdb401c6cfaa5019e3/Sources/AsyncValue/AsyncValue.swift#L120-L163)

---

<div class="post-metadata">

**Author:** ![stackotter](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stackotter/32/20670_2.png) [@stackotter](https://forums.swift.org/u/stackotter)\
**Post date:** [May 31, 2025, 11:39pm UTC](https://forums.swift.org/t/how-does-swiftui-find-state-properties-in-views/79984/25 "2025-05-31T23:39:08Z")

</div>

I believe that that requires the parent type to be a class, so it works for Published in ObservableObjects but not for State in View structs.

[Previous page](https://forums.swift.org/t/how-does-swiftui-find-state-properties-in-views/79984.md?page=1)
