Skip to content

Commit b4ab2eb

Browse files
authored
[RN][Doc] Passing initial props to iOS views (facebook#4563)
1 parent 6ba25f0 commit b4ab2eb

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

docs/_integration-with-existing-apps-ios.md

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,135 @@ REACT_NATIVE_XCODE="$REACT_NATIVE_PATH/scripts/react-native-xcode.sh"
516516

517517
Now, if you build your app for Release, it will work as expected.
518518

519-
### Now what?
519+
## 7. Passing initial props to the React Native view
520+
521+
In some case, you'd like to pass some information from the Native app to JavaScript. For example, you might want to pass the user id of the currently logged user to React Native, together with a token that can be used to retrieve information from a database.
522+
523+
This is possible by using the `initialProperties` parameter of the `view(withModuleName:initialProperty)` overload of the `RCTReactNativeFactory` class. The following steps shows you how to do it.
524+
525+
### Update the App.tsx file to read the initial properties.
526+
527+
Open the `App.tsx` file and add the following code:
528+
529+
```diff title="App.tsx"
530+
import {
531+
Colors,
532+
DebugInstructions,
533+
Header,
534+
ReloadInstructions,
535+
} from 'react-native/Libraries/NewAppScreen';
536+
537+
-function App(): React.JSX.Element {
538+
+function App(props): React.JSX.Element {
539+
const isDarkMode = useColorScheme() === 'dark';
540+
541+
const backgroundStyle = {
542+
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
543+
};
544+
545+
return (
546+
<SafeAreaView style={backgroundStyle}>
547+
<StatusBar
548+
barStyle={isDarkMode ? 'light-content' : 'dark-content'}
549+
backgroundColor={backgroundStyle.backgroundColor}
550+
/>
551+
<ScrollView
552+
contentInsetAdjustmentBehavior="automatic"
553+
style={backgroundStyle}>
554+
<Header />
555+
- <View
556+
- style={{
557+
- backgroundColor: isDarkMode
558+
- ? Colors.black
559+
- : Colors.white,
560+
- padding: 24,
561+
- }}>
562+
- <Text style={styles.title}>Step One</Text>
563+
- <Text>
564+
- Edit <Text style={styles.bold}>App.tsx</Text> to
565+
- change this screen and see your edits.
566+
- </Text>
567+
- <Text style={styles.title}>See your changes</Text>
568+
- <ReloadInstructions />
569+
- <Text style={styles.title}>Debug</Text>
570+
- <DebugInstructions />
571+
+ <Text style={styles.title}>UserID: {props.userID}</Text>
572+
+ <Text style={styles.title}>Token: {props.token}</Text>
573+
</View>
574+
</ScrollView>
575+
</SafeAreaView>
576+
);
577+
}
578+
579+
const styles = StyleSheet.create({
580+
title: {
581+
fontSize: 24,
582+
fontWeight: '600',
583+
+ marginLeft: 20,
584+
},
585+
bold: {
586+
fontWeight: '700',
587+
},
588+
});
589+
590+
export default App;
591+
```
592+
593+
These changes will tell React Native that your App component is now accepting some properties. The `RCTreactNativeFactory` will take care of passing them to the component when it's rendered.
594+
595+
### Update the Native code to pass the initial properties to JavaScript.
596+
597+
<Tabs groupId="ios-language" queryString defaultValue={constants.defaultAppleLanguage} values={constants.appleLanguages}>
598+
<TabItem value="objc">
599+
600+
Modify the `ReactViewController.mm` to pass the initial properties to JavaScript.
601+
602+
```diff title="ReactViewController.mm"
603+
- (void)viewDidLoad {
604+
[super viewDidLoad];
605+
// Do any additional setup after loading the view.
606+
607+
_factoryDelegate = [ReactNativeFactoryDelegate new];
608+
_factoryDelegate.dependencyProvider = [RCTAppDependencyProvider new];
609+
_factory = [[RCTReactNativeFactory alloc] initWithDelegate:_factoryDelegate];
610+
- self.view = [_factory.rootViewFactory viewWithModuleName:@"HelloWorld"];
611+
+ self.view = [_factory.rootViewFactory viewWithModuleName:@"HelloWorld" initialProperties:@{
612+
+ @"userID": @"12345678",
613+
+ @"token": @"secretToken"
614+
+ }];
615+
}
616+
```
617+
618+
</TabItem>
619+
<TabItem value="swift">
620+
621+
Modify the `ReactViewController.swift` to pass the initial properties to the React Native view.
622+
623+
```diff title="ReactViewController.swift"
624+
override func viewDidLoad() {
625+
super.viewDidLoad()
626+
reactNativeFactoryDelegate = ReactNativeDelegate()
627+
reactNativeFactoryDelegate!.dependencyProvider = RCTAppDependencyProvider()
628+
reactNativeFactory = RCTReactNativeFactory(delegate: reactNativeFactoryDelegate!)
629+
- view = reactNativeFactory!.rootViewFactory.view(withModuleName: "HelloWorld")
630+
+ view = reactNativeFactory!.rootViewFactory.view(withModuleName: "HelloWorld" initialProperties: [
631+
+ "userID": "12345678",
632+
+ "token": "secretToken"
633+
+])
634+
635+
}
636+
}
637+
```
638+
639+
</TabItem>
640+
</Tabs>
641+
642+
3. Run your app once again. You should see the following screen after you present the `ReactViewController`:
643+
644+
<center>
645+
<img src="/docs/assets/brownfield-with-initial-props.png" width="30%" height="30%"/>
646+
</center>
647+
648+
## Now what?
520649

521650
At this point you can continue developing your app as usual. Refer to our [debugging](debugging) and [deployment](running-on-device) docs to learn more about working with React Native.
290 KB
Loading

0 commit comments

Comments
 (0)