Update React Native App Tutorial authored by Aaditya Prakash's avatar Aaditya Prakash
......@@ -71,3 +71,205 @@ If you are using a simulator or emulator, you may find the following Expo CLI ke
![Tutorial_Image_01](uploads/cc1b4e765bb65a31354ac0e4ee8981ac/Tutorial_Image_01.png)
# Chapter 2. React Views and Components
#### Create a View
First you need to add some dependencies before we move on.
yarn add the following libraries
```javascript
yarn add react-native-paper react-native-safe-area-context
```
once installed run your project again with ``` expo start``` and add the following line of codes into your App.js
```javascript
...
import { SafeAreaProvider } from 'react-native-safe-area-context';
...
export default function App() {
return (
<SafeAreaProvider>
<Text>Hello World</Text>
</SafeAreaProvider>
);
}
```
The SafeAreaProvider will help us to keep our content inside the notches and status bar. It will not let any overlay that's why it's called "safe area"
After adding this we will add a view folder where we'll have all our views.
```javascript
mkdir views
cd views
```
Now create a new folder with the name "timetable-view" and add the following three files in it with right extensions.
![Screen_Shot_2021-11-29_at_2.04.06_PM](uploads/f88e95a37e78572da8b7549a8bcb39a0/Screen_Shot_2021-11-29_at_2.04.06_PM.png)
in package.json add the following line of code:
```javascript
{
"name": "@openasist/view-timetable",
"main": "index.js",
"dependencies": {
}
}
```
and in index.js add the following to get a view with a app bar in it
```javascript
import React from 'react';
import {
View,
SafeAreaView,
Text
} from 'react-native';
import {Appbar} from "react-native-paper";
/**
* Timetable View
*
* Sample View, not implemented at this moment
**/
class TimetableView extends React.Component {
static navigationOptions = {
header: null
};
constructor(props) {
super(props);
}
render() {
return (
<SafeAreaView style={{flex:1}}>
<Appbar style={{backgroundColor: "#005f50"}}>
<Appbar.Content title="Time Table" subtitle="" />
</Appbar>
</SafeAreaView>
);
}
}
export default TimetableView;
```
Now import and call the ```TimetableView``` component inside App.js file and in the SafeAreaProvider tags like this:
```
...
<SafeAreaProvider>
<TimetableView/>
</SafeAreaProvider>
```
The following should be the result:
![Simulator_Screen_Shot_-_iPhone_12_-_2021-11-29_at_13.51.11](uploads/fd50cb333aba1bc584c018521155e19a/Simulator_Screen_Shot_-_iPhone_12_-_2021-11-29_at_13.51.11.png)
now add the following line of code to get sense of the new view and how will it look:
```
...
render() {
return (
<SafeAreaView style={{flex:1}}>
...
<View style={{flex: 1, justifyContent: "center", alignItems:"center", backgroundColor: "grey"}}>
<Text style={{color: "white", fontSize:20}}>New View</Text>
</View>
</SafeAreaView>
);
}
```
A new view has been created and this is the way we'll create other views too.
![Simulator_Screen_Shot_-_iPhone_12_-_2021-11-29_at_13.52.30](uploads/d924ff8fe7a6c0ae8d234713677ceb7d/Simulator_Screen_Shot_-_iPhone_12_-_2021-11-29_at_13.52.30.png)
## React Navigation
on your terminal use command:
```
yarn add @react-navigation/native @react-navigation/native-stack
```
Now, we need to wrap the whole app in `NavigationContainer`. Usually you'd do this in your entry file, such as `index.js` or `App.js`:
```
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
export default function App() {
return (
<NavigationContainer>{/* Rest of your app code */}</NavigationContainer>
);
}
```
After that create your screen stack inside `App.js`:
```
import * as React from 'react';
...
import { createNativeStackNavigator } from '@react-navigation/native-stack';
...
const Stack = createNativeStackNavigator();
export default function App() {
return (
<SafeAreaProvider>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Timetable"
component={TimetableView}
options={{
headerShown: false
}}/>
</Stack.Navigator>
</NavigationContainer>
</SafeAreaProvider>
);
}
```
Now you can add as many views inside `Stack.Navigator` by using `Stack.Screen` for example now we create a new view so that we can use navigation to navigate between screens.
Create a view same as we did in last chapter and then add it in `Stack.Navigator` like this:
```
...
<Stack.Screen
name="TimetableDetail"
component={TimetableDetailView}
options={{
title: 'Overview',
headerStyle: styles.headerStyle,
headerTintColor: headerTintColor,
headerTitleStyle: styles.headerTitleStyle
}}/>
```
Also add the styles to support the above mentioned screen
```
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
headerStyle: {
backgroundColor: "#005f50",
},
headerTintColor: {
color: '#fff'
},
headerTitleStyle: {
fontWeight: 'bold',
}
});
```