Skip to content

Commit 27cf164

Browse files
committed
docs(swift): add UIKit store usage examples
1 parent 1f4be05 commit 27cf164

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

docs/docs/docs/api-reference/swift.mdx

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,81 @@ NavigationLink("Open React Native Screen") {
250250

251251
---
252252

253+
## Brownie Store (UIKit)
254+
255+
The `Store` class provides UIKit-friendly APIs for subscribing to state changes.
256+
257+
### Closure-based subscription
258+
259+
```swift
260+
import UIKit
261+
import Brownie
262+
263+
class MyViewController: UIViewController {
264+
private var store: Store<MyStoreState>?
265+
private var cancelSubscription: (() -> Void)?
266+
267+
override func viewDidLoad() {
268+
super.viewDidLoad()
269+
270+
store = StoreManager.get(key: MyStoreState.storeName, as: MyStoreState.self)
271+
272+
// Subscribe to all state changes
273+
cancelSubscription = store?.subscribe { [weak self] state in
274+
self?.updateUI(with: state)
275+
}
276+
}
277+
278+
deinit {
279+
cancelSubscription?()
280+
}
281+
}
282+
```
283+
284+
### Subscribe to specific property
285+
286+
```swift
287+
// Only fires when `counter` changes
288+
cancelSubscription = store?.subscribe(\.counter) { [weak self] counter in
289+
self?.counterLabel.text = "Count: \(counter)"
290+
}
291+
```
292+
293+
### Combine-based subscription
294+
295+
```swift
296+
import Combine
297+
298+
class MyViewController: UIViewController {
299+
private var store: Store<MyStoreState>?
300+
private var cancellables = Set<AnyCancellable>()
301+
302+
override func viewDidLoad() {
303+
super.viewDidLoad()
304+
305+
store = StoreManager.get(key: MyStoreState.storeName, as: MyStoreState.self)
306+
307+
store?.$state
308+
.sink { [weak self] state in
309+
self?.updateUI(with: state)
310+
}
311+
.store(in: &cancellables)
312+
}
313+
}
314+
```
315+
316+
### Updating state
317+
318+
```swift
319+
// Update with closure
320+
store?.set { $0.counter += 1 }
321+
322+
// Update specific property
323+
store?.set(\.user.name, to: "New Name")
324+
```
325+
326+
---
327+
253328
## Example
254329

255330
You can find an example consumer Apple platform app [here](https://github.com/callstack/react-native-brownfield/tree/main/apps/AppleApp) and the consumed library module [here](https://github.com/callstack/react-native-brownfield/tree/main/apps/RNApp/ios).

0 commit comments

Comments
 (0)