The navigation header

The navigation bars that you've created so far in this chapter have been sort of plain. That's because you haven't configured them to do anything, so react-navigation will just render a plain bar with a back button. Each screen component that you create can configure specific navigation header content.

Let's build on the previous example that used buttons to navigate to a details page. The App component stays the same, so let's take a look at the Home component first:

import React from 'react';
import { View, Button } from 'react-native';

import styles from './styles';

const Home = ({ navigation }) => (
<View style={styles.container}>
<Button
title="First Item"
onPress={() =>
navigation.navigate('Details', {
title: 'First Item',
content: 'First Item Content',
stock: 1
})
}
/>
<Button
title="Second Item"
onPress={() =>
navigation.navigate('Details', {
title: 'Second Item',
content: 'Second Item Content',
stock: 0
})
}
/>
<Button
title="Third Item"
onPress={() =>
navigation.navigate('Details', {
title: 'Third Item',
content: 'Third Item Content',
stock: 200
})
}
/>
</View>
);

Home.navigationOptions = {
title: 'Home'
};

export default Home;

The first thing you'll notice is that each button is passing more route parameters to the Details component: content and stock. You'll see why in a moment. It's the Home.navigationOptions value that configures the navigation header for you. In this case, the Home screen is setting the title.

The Home screen is a functional component, so you can just set navigationOptions as a property on the function. If your component is class based because it has the state of life cycle methods, you can define it as a static class property:

class MyScreen extends Component { static navigationOptions = {...} ... }

Next, let's take a look at the Details component:

import React from 'react';
import { View, Text, Button } from 'react-native';

import styles from './styles';

const Details = ({ navigation }) => (
<View style={styles.container}>
<Text>{navigation.getParam('content')}</Text>
</View>
);

Details.navigationOptions = ({ navigation }) => ({
title: navigation.getParam('title'),
headerRight: (
<Button
title="Buy"
onPress={() => {}}
disabled={navigation.getParam('stock') === 0}
/>
)
});

export default Details;

This time, the Details component renders the content route parameter. Like the Home component, it also has a navigationOptions property. In this case, it's a function instead of an object. This is because you're dynamically changing navigation header content based on the parameters that are passed to the screen. The function is passed a navigation property - this is the same value that's passed to the Details component. You can call navigation.getParam() to get the title to change the navigation header based on a route parameter.

Next, the headerRight option is used to add a Button component to the right side of the navigation bar. This is where the stock parameter comes into play. If this value is 0 because there isn't anything in stock, you want to disable the Buy button.

Let's see how all of this works now, starting with the Home screen:

There is how header text in the navigation bar, is set by the Home screen component. Next, try clicking on the First Item button:

The title in the navigation bar is set based on the title parameter that's passed to the Details component. The Buy button that's rendered on the right side of the navigation bar is rendered by the Details component as well. It's enabled because the stock parameter value is 1. Now try returning to the Home screen and clicking on the Second Item button:

The title and the page content both reflect the new parameter values passed to Details. But so does the Buy button. It is in a disabled state because the stock parameter value was 0, meaning that it can't be bought.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.32.86