Skip to content
DeveloperMemos

How to Use the onChange Modifier in SwiftUI

SwiftUI, State, ViewModifiers3 min read

SwiftUI provides a lot of built-in functionality to create interactive user interfaces, and one of the most important tools for this is the onChange modifier. This modifier allows us to execute a block of code whenever a specific state variable changes. In this article, we will explore how to use the onChange modifier in SwiftUI and some common use cases for it.

Syntax of onChange

The syntax of the onChange modifier is straightforward. It takes two arguments: the state variable we want to monitor and the closure to execute whenever the state variable changes. Here's the basic syntax:

1.onChange(of: someStateVariable) { newValue in
2 // Perform some action when `someStateVariable` changes
3}

The closure that we pass to the onChange modifier receives the new value of the state variable as its parameter. We can use this value to perform some action, update the UI, or trigger some other functionality in our app.

Common use cases for onChange

Updating the UI

One common use case for onChange is to update the UI whenever a state variable changes. For example, we might have a view that displays some text based on a user's input. We can use onChange to update this text whenever the user types something new:

1@State private var userInput = ""
2
3var body: some View {
4 TextField("Enter some text", text: $userInput)
5 .onChange(of: userInput) { newValue in
6 updateText()
7 }
8}
9
10func updateText() {
11 // Update the text that is displayed in the view based on `userInput`
12}

SwiftUI provides a lot of built-in functionality to create interactive user interfaces, and one of the most important tools for this is the onChange modifier. This modifier allows us to execute a block of code whenever a specific state variable changes. In this article, we will explore how to use the onChange modifier in SwiftUI and some common use cases for it.

Syntax of onChange

The syntax of the onChange modifier is straightforward. It takes two arguments: the state variable we want to monitor and the closure to execute whenever the state variable changes. Here's the basic syntax:

1.onChange(of: someStateVariable) { newValue in
2 // Perform some action when `someStateVariable` changes
3}

The closure that we pass to the onChange modifier receives the new value of the state variable as its parameter. We can use this value to perform some action, update the UI, or trigger some other functionality in our app.

Common use cases for onChange

Updating the UI

One common use case for onChange is to update the UI whenever a state variable changes. For example, we might have a view that displays some text based on a user's input. We can use onChange to update this text whenever the user types something new:

1@State private var userInput = ""
2
3var body: some View {
4 TextField("Enter some text", text: $userInput)
5 .onChange(of: userInput) { newValue in
6 updateText()
7 }
8}
9
10func updateText() {
11 // Update the text that is displayed in the view based on `userInput`
12}

In this example, we use onChange to call the updateText() function whenever the value of userInput changes. This function will then update the text that is displayed in the view based on the new value of userInput.

Triggering a network request

Another common use case for onChange is to trigger a network request whenever a state variable changes. For example, we might have a view that displays some data from a server. We can use onChange to fetch new data from the server whenever the user interacts with the view:

1@State private var data: [SomeData] = []
2
3var body: some View {
4 List(data) { item in
5 Text(item.title)
6 }
7 .onAppear {
8 fetchData()
9 }
10 .onChange(of: someStateVariable) { newValue in
11 fetchData()
12 }
13}
14
15func fetchData() {
16 // Fetch new data from the server and update the `data` state variable
17}

In this example, we use onChange to call the fetchData() function whenever the value of someStateVariable changes. This function will then fetch new data from the server and update the data state variable, which will cause the UI to update automatically.

Responding to user actions

Finally, we can use onChange to respond to user actions in our app. For example, we might have a view that allows the user to select some options. We can use onChange to update the UI or perform some action whenever the user makes a selection:

1@State private var selectedOption: Int = 0
2
3var body: some View {
4 VStack {
5 Picker("Select an option", selection: $selectedOption) {
6 Text("Option 1").tag(0)
7 Text("Option 2").tag(1)
8 Text("Option 3").tag(2)
9 }
10 }
11 .onChange(of: selectedOption) { newValue in
12 performAction()
13 }
14}
15
16func performAction() {
17 // Perform some action based on the selected option
18}

In this example, we use onChange to call the performAction() function whenever the value of selectedOption changes. This function will then perform some action based on the selected option, such as updating the UI, triggering a network request, or changing the app's state.

Additional Notes

It's important to note that onChange is only triggered when the value of the state variable changes. If we try to update the state variable to the same value it already has, the closure passed to onChange won't be executed. In addition, onChange is only available in SwiftUI 2.0 and later, so make sure your app is using the latest version of SwiftUI.

Conclusion

The onChange modifier is a powerful tool that allows us to respond to changes in state variables in SwiftUI. It can be used to update the UI, trigger network requests, and respond to user actions. By understanding the syntax and common use cases for onChange, we can make our SwiftUI apps more dynamic and interactive.