Skip to content

Commit c5386e2

Browse files
committed
feat(demo): demonstrate initial props from native to root RN component
1 parent 0d9005c commit c5386e2

7 files changed

Lines changed: 97 additions & 10 deletions

File tree

apps/AndroidApp/app/src/main/java/com/callstack/brownfield/android/example/MainActivity.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.callstack.brownfield.android.example
22

33
import android.content.Intent
44
import android.content.res.Configuration
5+
import android.os.Build
56
import android.os.Bundle
67
import android.widget.Toast
78
import androidx.activity.compose.setContent
@@ -129,6 +130,15 @@ fun ReactNativeView(
129130
ReactNativeFragmentArgNames.ARG_MODULE_NAME,
130131
ReactNativeConstants.MAIN_MODULE_NAME
131132
)
133+
putBundle(
134+
ReactNativeFragmentArgNames.ARG_LAUNCH_OPTIONS,
135+
Bundle().apply {
136+
putString(
137+
"nativeOsVersionLabel",
138+
"Android ${Build.VERSION.RELEASE}"
139+
)
140+
}
141+
)
132142
}
133143
)
134144
}

apps/AppleApp/Brownfield Apple App/components/ContentView.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ struct ContentView: View {
2323

2424
MessagesView()
2525

26-
ReactNativeView(moduleName: "main")
26+
ReactNativeView(
27+
moduleName: "main",
28+
initialProperties: [
29+
"nativeOsVersionLabel":
30+
"\(UIDevice.current.systemName) \(UIDevice.current.systemVersion)"
31+
]
32+
)
2733
.navigationBarHidden(true)
2834
.clipShape(RoundedRectangle(cornerRadius: 16))
2935
.background(Color(UIColor.systemBackground))

apps/ExpoApp54/RNApp.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@ import BrownfieldNavigation from '@callstack/brownfield-navigation';
44

55
import Counter from './components/counter';
66

7-
export default function RNApp() {
7+
type RNAppProps = {
8+
nativeOsVersionLabel?: string;
9+
};
10+
11+
export default function RNApp({ nativeOsVersionLabel }: RNAppProps) {
812
return (
913
<SafeAreaView style={styles.container}>
1014
<Text style={styles.title}>Expo React Native Brownfield</Text>
1115

16+
{nativeOsVersionLabel ? (
17+
<Text
18+
style={styles.nativeOsVersionLabel}
19+
accessibilityLabel="Native OS version"
20+
>
21+
{nativeOsVersionLabel}
22+
</Text>
23+
) : null}
24+
1225
<View style={styles.content}>
1326
<Counter />
1427

@@ -36,6 +49,12 @@ const styles = StyleSheet.create({
3649
fontWeight: 'bold',
3750
textAlign: 'center',
3851
},
52+
nativeOsVersionLabel: {
53+
fontSize: 11,
54+
opacity: 0.75,
55+
textAlign: 'center',
56+
marginTop: 4,
57+
},
3958
content: {
4059
flex: 1,
4160
justifyContent: 'center',

apps/ExpoApp55/RNApp.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@ import BrownfieldNavigation from '@callstack/brownfield-navigation';
44

55
import Counter from './src/components/counter';
66

7-
export default function RNApp() {
7+
type RNAppProps = {
8+
nativeOsVersionLabel?: string;
9+
};
10+
11+
export default function RNApp({ nativeOsVersionLabel }: RNAppProps) {
812
return (
913
<SafeAreaView style={styles.container}>
1014
<Text style={styles.title}>Expo React Native Brownfield</Text>
1115

16+
{nativeOsVersionLabel ? (
17+
<Text
18+
style={styles.nativeOsVersionLabel}
19+
accessibilityLabel="Native OS version"
20+
>
21+
{nativeOsVersionLabel}
22+
</Text>
23+
) : null}
24+
1225
<View style={styles.content}>
1326
<Counter />
1427

@@ -36,6 +49,12 @@ const styles = StyleSheet.create({
3649
fontWeight: 'bold',
3750
textAlign: 'center',
3851
},
52+
nativeOsVersionLabel: {
53+
fontSize: 11,
54+
opacity: 0.75,
55+
textAlign: 'center',
56+
marginTop: 4,
57+
},
3958
content: {
4059
flex: 1,
4160
justifyContent: 'center',

apps/RNApp/src/App.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@ import '../BrownfieldStore.brownie';
22

33
import { NavigationContainer } from '@react-navigation/native';
44

5-
import { Stack } from './navigation/RootStack';
65
import { HomeScreen } from './HomeScreen';
6+
import { NativeOsVersionLabelContext } from './nativeHostContext';
7+
import { Stack } from './navigation/RootStack';
8+
9+
type AppProps = {
10+
nativeOsVersionLabel?: string;
11+
};
712

8-
export default function App() {
13+
export default function App({ nativeOsVersionLabel }: AppProps) {
914
return (
10-
<NavigationContainer>
11-
<Stack.Navigator>
12-
<Stack.Screen name="Home" component={HomeScreen} />
13-
</Stack.Navigator>
14-
</NavigationContainer>
15+
<NativeOsVersionLabelContext.Provider value={nativeOsVersionLabel}>
16+
<NavigationContainer>
17+
<Stack.Navigator>
18+
<Stack.Screen name="Home" component={HomeScreen} />
19+
</Stack.Navigator>
20+
</NavigationContainer>
21+
</NativeOsVersionLabelContext.Provider>
1522
);
1623
}

apps/RNApp/src/HomeScreen.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import BrownfieldNavigation from '@callstack/brownfield-navigation';
1515
import { getRandomTheme } from './utils';
1616
import type { RootStackParamList } from './navigation/RootStack';
1717
import Counter from './components/counter';
18+
import { useNativeOsVersionLabel } from './nativeHostContext';
1819

1920
interface Message {
2021
id: string;
@@ -71,6 +72,7 @@ export function HomeScreen({
7172
navigation,
7273
route,
7374
}: NativeStackScreenProps<RootStackParamList, 'Home'>) {
75+
const nativeOsVersionLabel = useNativeOsVersionLabel();
7476
const colors = route.params?.theme ? route.params.theme : getRandomTheme();
7577
const [messages, setMessages] = useState<Message[]>([]);
7678
const flatListRef = useRef<FlatList<Message>>(null);
@@ -122,6 +124,15 @@ export function HomeScreen({
122124
React Native Screen
123125
</Text>
124126

127+
{nativeOsVersionLabel ? (
128+
<Text
129+
style={[styles.nativeOsVersionLabel, { color: colors.secondary }]}
130+
accessibilityLabel="Native OS version"
131+
>
132+
{nativeOsVersionLabel}
133+
</Text>
134+
) : null}
135+
125136
<Counter colors={colors} />
126137

127138
<View style={styles.messageSection}>
@@ -199,6 +210,12 @@ const styles = StyleSheet.create({
199210
fontWeight: 'bold',
200211
marginBottom: 8,
201212
},
213+
nativeOsVersionLabel: {
214+
fontSize: 11,
215+
opacity: 0.85,
216+
marginBottom: 4,
217+
textAlign: 'center',
218+
},
202219
messageSection: {
203220
flex: 1,
204221
width: '100%',
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createContext, useContext } from 'react';
2+
3+
export const NativeOsVersionLabelContext = createContext<string | undefined>(
4+
undefined
5+
);
6+
7+
export function useNativeOsVersionLabel(): string | undefined {
8+
return useContext(NativeOsVersionLabelContext);
9+
}

0 commit comments

Comments
 (0)