lecture 6: navigation - cs50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdflecture 6:...

47
Lecture 6: Navigation Brent Vatne & Eric Vicenti

Upload: others

Post on 30-May-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Lecture 6:Navigation

Brent Vatne & Eric Vicenti

Page 2: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Previous Lecture● User input with TextInput● Simple input validation● KeyboardAvoidingView

● Debugging○ Errors and warnings○ Chrome Developer Tools○ React Native Inspector with react-devtools

● Installing external libraries with npm

Page 3: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

What is navigation?● Navigation is a broad term that covers topics related to how you move

between screens in your app● Web navigation is oriented around URLs● Mobile apps do not use URLs for navigating within the app*● Navigation APIs completely different on iOS and Android

○ Several React Native libraries provide a platform agnostic alternative○ We will talk about one of them today, React Navigation

* Linking into a mobile app with a URL is known as deep linking: https://v2.reactnavigation.org/docs/deep-linking.html

Page 7: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

React Navigation and alternatives● Two distinct approaches

1. Implement mainly in JavaScript with React2. Implement mostly in native, expose an interface to JavaScript for existing

native navigation APIs● React Navigation takes approach #1

Read more at https://v2.reactnavigation.org/docs/pitch.html and https://v2.reactnavigation.org/docs/alternatives.html

Page 8: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Install React Navigation● npm install [email protected] --save

● This will install the latest pre-release version at the time of writing. Typically you would just write npm install react-navigation --save to use the latest stable version.

● If you refer back to this in the future, keep in mind that this material is all specific to the 2.x series of releases.

Page 9: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Navigators, routes, and screen components● A navigator is a component that implements a navigation pattern (eg: tabs)● Each navigator must have one or more routes.

○ A navigator is a parent of a route.○ A route is a child of a navigator.

● Each route must have a name and a screen component.○ The name is usually unique across the app○ The screen component is a React component that is rendered when the route

is active.○ The screen component can also be another navigator.

Page 10: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Switch Navigator● Display one screen at a time● Inactive screens are unmounted● The only action a user can take to switch from one route to another

Page 11: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Switch Navigator

Screen one

Go to two

Screen two

Go to one

Page 12: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Creating a navigator

import { createSwitchNavigator } from 'react-navigation';

const AppNavigator = createSwitchNavigator({

"RouteNameOne": ScreenComponentOne,

"RouteNameTwo": ScreenComponentTwo,

});

Page 13: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Rendering a navigator

● createSwitchNavigator is a function that returns a React component● We render the component in our root App component. Usually we only explicitly

render one navigator per app because navigators are composable.

const AppNavigator = createSwitchNavigator({

"RouteNameOne": ScreenComponentOne,

"RouteNameTwo": ScreenComponentTwo,

});

export default class App extends React.Component {

render() {

return <AppNavigator />

}

}

Page 14: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Higher order components● createSwitchNavigator is a Higher Order Component: it is a function that

returns a React component.● “A higher-order component (HOC) is an advanced technique in React for

reusing component logic.”● This is similar to higher order functions, which are functions that either take

functions as arguments or return a function as a result.

Read more at https://reactjs.org/docs/higher-order-components.html

Page 15: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Navigating to another route

class ScreenComponentOne extends React.Component {

render() {

return (

<Button

title="Go to two"

onPress={() => this.props.navigation.navigate('RouteNameTwo')}

/>

);

}

}

Page 16: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

The navigation prop● navigate(..)

● goBack(..)

● setParams(..)

● getParam(..)

● dispatch(..)

● isFocused(..)

● addListener(..)

● state

* The navigation prop is passed in to the screen component for each route.

Full reference: https://v2.reactnavigation.org/docs/navigation-prop.html

Page 17: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple
Page 18: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple
Page 19: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

screenProps

● Made available to every screen component in the navigator.● Perfectly fine for very small applications and prototyping but inefficient for most

meaningful applications - every route in your app will re-render when screenProps changes. Use a state management library or the React Context API instead.

export default class App extends React.Component {

render() {

return <AppNavigator screenProps={/* object here */} />

}

}

Page 20: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Stack Navigator● Display one screen at a time● The state of inactive screens is maintained and they remain mounted● Platform-specific layout, animations, and gestures

○ Screens are stacked on top of each other○ iOS: screens slide in from right to left, can be dismissed with left to right

gesture. Modal screens slide in from bottom to top, can be dismissed with top to bottom gesture.

○ Android: screens fade in on top of each other, no dismiss gesture. Hardware back button dismisses the active screen.

● Users can push and pop items from the stack, replace the current item, and various other

Page 21: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Stack Navigator

Screen one

Go to two

Screen two

Go to threeScreen three

Page 22: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Stack Navigator

Screen one

Go to two

Screen two

Go to threeScreen three

Page 23: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Creating a StackNavigatorimport { createStackNavigator } from 'react-navigation';

const AppNavigator = createStackNavigator({

"RouteNameOne": ScreenComponentOne,

"RouteNameTwo": ScreenComponentTwo,

});

Page 24: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Navigating to another route

class ScreenComponentOne extends React.Component {

render() {

return (

<Button

title="Go to two"

onPress={() => this.props.navigation.navigate('RouteNameTwo')}

/>

);

}

}

Page 25: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Returning to the previously active route

class ScreenComponentThree extends React.Component {

render() {

return (

<Button

title="Go back"

onPress={() => this.props.navigation.goBack()}

/>

);

}

}

Page 26: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple
Page 27: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Configuring navigationOptions● headerTitle

● headerStyle

● headerTintColor

● headerLeft

● headerRight

Full list: https://v2.reactnavigation.org/docs/stack-navigator.html#navigationoptions...

Page 28: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Using params to pass state between routes● navigate with params

this.props.navigation.navigate('RouteName', {

paramName: 'value-of-param'

});

● setParams to update params for the route

this.props.navigation.setParams({

paramName: 'new-value-of-param',

});

● getParam to read a param

this.props.navigation.getParam('paramName', 'default-value');

Page 29: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

// It's time for us to take a short break

this.props.navigation.navigate('BreakTime');

Page 30: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

// Break time is over

this.props.navigation.goBack();

Page 31: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple
Page 32: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Add button to header with navigationOptions ● headerLeft

● headerRight

Full list: https://v2.reactnavigation.org/docs/stack-navigator.html#navigationoptions...

Page 33: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

// Jump to a screen, identified by route name

navigate('MyRouteName', { paramName: 'param-value' });

// “Push” a new screen, even if it already is in the stack

push('MyRouteName');

Page 34: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Stack specific navigation actions● push(..)

● pop(..)

● popToTop(..)

● replace(..)

More information: https://v2.reactnavigation.org/docs/navigation-prop.html#...

Page 35: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Composing navigators● Navigators can be composed when one type of navigation visually appears to

be inside another navigator● A navigator can be the Screen Component of another navigator● The app should only contain one top-level navigator● You can navigate() to any route in the app● goBack() works for the whole app, supports Android back button

Page 36: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Composing navigators const MyStackNavigator = createStackNavigator({

"Home": HomeScreen,

"AddContact": AddContactScreen,

});

const AppNavigator = createSwitchNavigator({

"Login": LoginScreen,

"Main": MyStackNavigator,

});

Page 37: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Do not render a navigator inside a screenclass MyScreen extends React.Component {

render() {

return <MyStackNavigator />;

}

}

const AppNavigator = createSwitchNavigator({

"Main": MyStackNavigator,

});

Instead, set as a screen within the AppNavigator

Page 38: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple
Page 39: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Tab navigators● Display one screen at a time● The state of inactive screens is maintained● Platform-specific layout, animations, and gestures

○ createMaterialTopTabNavigator

○ createMaterialBottomTabNavigator

○ createBottomTabNavigator

● The navigate() action is used to switch to different tabs● goBack() can be called to go back to the first tab

○ The tab navigator goBack behavior is configurable

Page 40: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Screen one

One Two

Tab navigators

Screen two

One Two

Page 41: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Screen one

Two

Tab navigators

Screen two

One Two

Page 42: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Creating a tab navigatorconst AppNavigator = createBottomTabNavigator({

"TabOne": ScreenComponentOne,

"TabTwo": ScreenComponentTwo,

});

export default class App extends React.Component {

render() {

return <AppNavigator />

}

}

Page 43: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple
Page 44: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Configure tab bar settingsconst MainTabs = createBottomTabNavigator(

{

...

},

{

tabBarOptions: {

activeTintColor: "#a41034"

}

}

);

Full reference for tabBarOptions: https://v2.reactnavigation.org/docs/tab-navigator.html#...

Page 45: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Configure tab iconsMainStack.navigationOptions = {

tabBarIcon: ({ focused, tintColor }) => (

<Ionicons

name={`ios-contacts${focused ? "" : "-outline"}`}

size={25}

color={tintColor}

/>

)

};

Full reference of options: https://v2.reactnavigation.org/docs/tab-navigator.html#...

Page 46: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

Use common icon packs

# Install it in your shellnpm install --save react-native-vector-icons

// Import a supported icon set in your code

import Ionicons from "react-native-vector-icons/Ionicons";

// Use it as a React component

<Ionicons name="md-checkmark" size={25} color="#000" />

See other icon sets that are included: https://expo.github.io/vector-icons/

Page 47: Lecture 6: Navigation - CS50cdn.cs50.net/mobile/2018/spring/lectures/6/lecture6.pdfLecture 6: Navigation Brent Vatne & Eric Vicenti. Previous Lecture User input with TextInput Simple

React Navigation Resources- React Navigation Documentation- React Navigation API Reference- NavigationPlayground example source code