diff --git a/docs/guides/Guide-Basic-Example.md b/docs/guides/Guide-Basic-Example.md new file mode 100644 index 0000000..a6721e0 --- /dev/null +++ b/docs/guides/Guide-Basic-Example.md @@ -0,0 +1,235 @@ +# Hello Mobile Navigation + +Let's use React Navigation to build a simple chat-like application for Android and iOS. + +## Setup and Installation + +First, make sure you're [all set up to use React Native](http://facebook.github.io/react-native/docs/getting-started.html). Next, create a new project and add `react-navigation`: + +```sh +# Create a new React Native App +react-native init SimpleApp +cd SimpleApp + +# Install the latest version of react-navigation from npm +npm install --save react-navigation + +# Run the new app +react-native run-android +# or: +react-native run-ios +``` + +If you are using `create-react-native-app` instead of `react-native init`, then: + +```sh +# Create a new React Native App +create-react-native-app SimpleApp +cd SimpleApp + +# Install the latest version of react-navigation from npm +npm install --save react-navigation + +# Run the new app +npm start + +# This will start a development server for you and print a QR code in your terminal. +``` + +Verify that you can successfully see the bare sample app run on iOS and/or Android: + +```phone-example +bare-project +``` + +We want to share code on iOS and Android, so let's delete the contents of `index.js` (or `index.ios.js` and `index.android.js` if using a React Native version before 0.49) and replace it with `import './App';` - after which, we need to create the new file for our app implementation, `App.js` (if you used `create-react-native-app` this has been already done) + +## Introducing Stack Navigator + +For our app, we want to use the `StackNavigator` because conceptually we want to obtain a 'card stack' effect of movement, where each new screen is put on the top of the stack and going back removes a screen from the top of the stack. Let's start with just one screen: + +```js +import React from 'react'; +import { + AppRegistry, + Text, +} from 'react-native'; +import { StackNavigator } from 'react-navigation'; + +class HomeScreen extends React.Component { + static navigationOptions = { + title: 'Welcome', + }; + render() { + return Hello, Navigation!; + } +} + +export const SimpleApp = StackNavigator({ + Home: { screen: HomeScreen }, +}); + +AppRegistry.registerComponent('SimpleApp', () => SimpleApp); +``` + +If you used `create-react-native-app` the already existing `App.js` will be modified to + +```js +import React from 'react'; +import { StyleSheet, Text, View } from 'react-native'; +import { StackNavigator } from 'react-navigation'; + +class HomeScreen extends React.Component { + static navigationOptions = { + title: 'Welcome' + }; + render() { + return Hello, Navigation!; + } +} + +const SimpleApp = StackNavigator({ + Home: { screen: HomeScreen } +}); + +export default class App extends React.Component { + render() { + return ; + } +} + +const styles = StyleSheet.create({ + container: { + flex: 1, + backgroundColor: '#fff', + alignItems: 'center', + justifyContent: 'center' + } +}); + +``` + +The `title` of the screen is configurable on the [static `navigationOptions`](/docs/navigators/navigation-options), where many options can be set to configure the presentation of the screen in the navigator. + +Now the same screen should appear on both iPhone and Android apps: + +```phone-example +first-screen +``` + +## Adding a New Screen + +In our `App.js` file, let's add a new screen called `ChatScreen`, defining it under `HomeScreen`: + +```js +// ... + +class HomeScreen extends React.Component { + //... +} + +class ChatScreen extends React.Component { + static navigationOptions = { + title: 'Chat with Lucy', + }; + render() { + return ( + + Chat with Lucy + + ); + } +} + +``` + +We can then add a button to our `HomeScreen` component that links to `ChatScreen`: we need to use the provided method `navigate` (from the [screen navigation prop](/docs/navigators/navigation-prop)) by giving it the `routeName` of the screen we want to reach, in this case `Chat`. + +```js +class HomeScreen extends React.Component { + static navigationOptions = { + title: 'Welcome', + }; + render() { + const { navigate } = this.props.navigation; + return ( + + Hello, Chat App! +