Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tunous/environmentactions
Extension to store actions in SwiftUI environment
https://github.com/tunous/environmentactions
swiftui
Last synced: 10 days ago
JSON representation
Extension to store actions in SwiftUI environment
- Host: GitHub
- URL: https://github.com/tunous/environmentactions
- Owner: Tunous
- License: mit
- Created: 2023-12-26T17:47:55.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2023-12-27T11:17:00.000Z (11 months ago)
- Last Synced: 2024-10-08T06:22:00.519Z (about 1 month ago)
- Topics: swiftui
- Language: Swift
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# EnvironmentActions
Set of structs and extensions that can be used to store actions inside of SwiftUI environment without causing unnecessary redraws of depending views.
## Usage
1. Add `EnvironmentKey` and extension on `EnvironmentValues` with custom action.
```swift
import SwiftUI
import EnvironmentActionsextension EnvironmentValues {
var exampleAction: EnvironmentAction {
get { self[ExampleActionKey.self] }
set { self[ExampleActionKey.self] = newValue }
}
}private struct ExampleActionKey: EnvironmentKey {
static var defaultValue: EnvironmentAction = .doNothing
}
```2. Define in parent view what will happen when the action executes.
```swift
struct ContentView: View {
var body: some View {
ChildView().environmentAction(\.exampleAction) { param in
print("Executed with parameter \(param)")
}
}
}
```3. Access the action in child views.
```swift
struct ChildView: View {
@Environment(\.exampleAction) private var exampleAction
var body: some View {
Button("Perform") {
exampleAction("param")
}
}
}
```