|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +This guide walks you through setting up Brownie from scratch - defining stores, generating native types, building XCFrameworks, and using shared state in both React Native and Swift. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before starting, ensure you have: |
| 8 | + |
| 9 | +- Completed the [React Native Brownfield Quick Start](/docs/getting-started/quick-start) guide |
| 10 | +- React Native project with `@callstack/react-native-brownfield` installed |
| 11 | +- Native iOS app (SwiftUI or UIKit) |
| 12 | +- Xcode 15+ |
| 13 | + |
| 14 | +## Step 1: Install Brownie |
| 15 | + |
| 16 | +In your React Native project: |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install @callstack/brownie |
| 20 | +``` |
| 21 | + |
| 22 | +## Step 2: Define Your Store |
| 23 | + |
| 24 | +Create a store definition file with the `.brownie.ts` extension: |
| 25 | + |
| 26 | +```ts |
| 27 | +// src/stores/AppStore.brownie.ts |
| 28 | +import type { BrownieStore } from '@callstack/brownie'; |
| 29 | + |
| 30 | +interface AppStore extends BrownieStore { |
| 31 | + counter: number; |
| 32 | +} |
| 33 | + |
| 34 | +declare module '@callstack/brownie' { |
| 35 | + interface BrownieStores { |
| 36 | + AppStore: AppStore; |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +Import the store in your app entry point: |
| 42 | + |
| 43 | +```ts |
| 44 | +// App.tsx |
| 45 | +import './stores/AppStore.brownie'; |
| 46 | +``` |
| 47 | + |
| 48 | +## Step 3: Use the Store in React Native |
| 49 | + |
| 50 | +```tsx |
| 51 | +import { useStore } from '@callstack/brownie'; |
| 52 | + |
| 53 | +function Counter() { |
| 54 | + const [counter, setState] = useStore('AppStore', (s) => s.counter); |
| 55 | + |
| 56 | + return ( |
| 57 | + <View> |
| 58 | + <Text>Count: {counter}</Text> |
| 59 | + <Button |
| 60 | + title="Increment" |
| 61 | + onPress={() => setState((prev) => ({ counter: prev.counter + 1 }))} |
| 62 | + /> |
| 63 | + </View> |
| 64 | + ); |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Step 4: Build XCFrameworks |
| 69 | + |
| 70 | +The CLI generates native types and builds everything into XCFrameworks: |
| 71 | + |
| 72 | +```bash |
| 73 | +npx brownfield package:ios --scheme YourScheme --configuration Release |
| 74 | +``` |
| 75 | + |
| 76 | +This produces the following in `ios/.brownfield/package/`: |
| 77 | + |
| 78 | +- `YourScheme.xcframework` - Your React Native module |
| 79 | +- `Brownie.xcframework` - Shared state library |
| 80 | +- `ReactBrownfield.xcframework` - Brownfield integration |
| 81 | +- `hermesvm.xcframework` - JavaScript engine (or `hermes.xcframework` for RN < 0.82.0) |
| 82 | + |
| 83 | +:::info |
| 84 | +The `package:ios` command automatically runs `brownfield codegen` to generate Swift types from your `.brownie.ts` files. |
| 85 | +::: |
| 86 | + |
| 87 | +## Step 5: Add Frameworks to Native App |
| 88 | + |
| 89 | +1. Open your native Xcode project |
| 90 | +2. Drag all XCFrameworks from `ios/.brownfield/package/` into your project |
| 91 | +3. In target settings → **General** → **Frameworks, Libraries, and Embedded Content**, set all to **Embed & Sign** |
| 92 | + |
| 93 | +## Step 6: Register Store in Swift |
| 94 | + |
| 95 | +In your app's entry point, register the store with initial state: |
| 96 | + |
| 97 | +```swift |
| 98 | +import Brownie |
| 99 | +import ReactBrownfield |
| 100 | +import SwiftUI |
| 101 | + |
| 102 | +let initialState = AppStore(counter: 0) |
| 103 | + |
| 104 | +@main |
| 105 | +struct MyApp: App { |
| 106 | + init() { |
| 107 | + ReactNativeBrownfield.shared.startReactNative { |
| 108 | + print("React Native loaded") |
| 109 | + } |
| 110 | + |
| 111 | + AppStore.register(initialState) |
| 112 | + } |
| 113 | + |
| 114 | + var body: some Scene { |
| 115 | + WindowGroup { |
| 116 | + ContentView() |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## Step 7: Use the Store in SwiftUI |
| 123 | + |
| 124 | +```swift |
| 125 | +import Brownie |
| 126 | +import SwiftUI |
| 127 | + |
| 128 | +struct CounterView: View { |
| 129 | + @UseStore(\AppStore.counter) var counter |
| 130 | + |
| 131 | + var body: some View { |
| 132 | + VStack { |
| 133 | + Text("Count: \(Int(counter))") |
| 134 | + Button("Increment") { |
| 135 | + $counter.set { $0 + 1 } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +## Done! |
| 143 | + |
| 144 | +State changes now sync automatically between React Native and Swift. Increment from either side and both update immediately. |
| 145 | + |
| 146 | +## Next Steps |
| 147 | + |
| 148 | +- [Defining Stores](/brownie/store-definition) - Nested objects and multiple stores |
| 149 | +- [TypeScript Usage](/brownie/typescript-usage) - Advanced React Native patterns |
| 150 | +- [Swift Usage](/brownie/swift-usage) - UIKit integration and API reference |
| 151 | +- [XCFramework Packaging](/brownie/xcframework-packaging) - SPM distribution and advanced options |
0 commit comments