-
Create a new react native project
react-native init AwesomeProject
cd AwesomeProject
-
Run your iOS app
react-native run-ios
-
Add folder
src/components/screens
-
Add ViewOne
import React, { Component } from 'react'
import {
View,
Text,
StyleSheet,
} from 'react-native'
class ViewOne extends Component {
constructor(props) {
super(props);
this.state = {
example: false,
};
}
render() {
return (
<View style={ styles.container }>
<Text>Hello</Text>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: "pink",
},
})
export default ViewOne
- Add index.js
import ViewOne from './ViewOne';
export {
ViewOne,
};
- Import our view
import { ViewOne } from "./src/components/screens";
- Update render
<View style={styles.container}>
<ViewOne />
</View>
-
import
TouchableOpacity
-
Update render
<TouchableOpacity
onPress={() => alert('hi')}
>
<Text>Hello</Text>
</TouchableOpacity>
-
Duplicate ViewOne
-
Edit ViewTwo.js & change references from
ViewOne
toViewTwo
-
Add
ViewTwo
toindex.js
-
Switch Main to target view (App.js)
import { ViewOne, ViewTwo } from "./src/components/screens";
- Update render in App.js
<ViewTwo />
Visit Getting started · React Navigation
- Stop server and install
yarn add react-navigation
yarn add react-native-gesture-handler
react-native link react-native-gesture-handler
- Create stack navigator
import { createAppContainer, createStackNavigator } from 'react-navigation';
- Create routes for StackNavigator
const RootStack = createStackNavigator(
{
ViewOne: {
screen: ViewOne,
navigationOptions: {
},
},
ViewTwo: {
screen: ViewTwo,
navigationOptions: {
},
},
}, {
initialRouteName: 'ViewOne',
}
);
-
Create AppContainer
const AppContainer = createAppContainer(RootStack);
-
Update render (Remove surrounding view)
<AppContainer />
- Bring in withNavigation to ViewOne.js
import { withNavigation } from 'react-navigation';
- Update the export in ViewOne.js
export default withNavigation(ViewOne);
- Update our onPress in ViewOne.js
this.props.navigation.push('ViewTwo', { data: 'Hello' })
This is a great example creating an instagram clone: Turbo 360 | Learn Node, React, Redux with Real World Project Tutorials.