refreshable

Adds an asynchronous handler that can update the data the view displays when a person initiates a request, such as by pulling to refresh.

View ModifieriOS 15.0+macOS 12.0+tvOS 15.0+watchOS 8.0+refreshable(action:) in SwiftUI

Declaration

React
compose().refreshable({
  action: () => void,
})
Swift
nonisolated func refreshable(
    action: @escaping @Sendable () async -> Void
) -> some View

Parameters

  • options.action*() => void

    An asynchronous handler that SwiftUI executes when a person requests a refresh. Use this handler to initiate an update of model data displayed in the modified view. Use await in front of any asynchronous calls inside the handler.

Return value

A view with a new refresh action in its environment.

Discussion

Apply this modifier to a view to set the EnvironmentValues.refresh value in the view's environment to a RefreshAction instance that uses the specified action as its handler. Views that detect the presence of the instance can change their appearance to provide a way for a person to execute the handler.

For example, when you apply this modifier on iOS and iPadOS to a List, the list enables a standard pull-to-refresh gesture that refreshes the list contents. When a person drags the top of the scrollable area downward, the view reveals a progress indicator and executes the specified handler. The indicator remains visible for the duration of the refresh, which runs asynchronously:

List(mailbox.conversations) { conversation in
    ConversationCell(conversation)
}
.refreshable {
    await mailbox.fetch()
}

You can add refresh capability to your own views as well. For information on how to do that, see RefreshAction.

See also

More in Lists and tables.