-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for badges from the NavigationComponent (#72)
* Support for badges from the NavigationComponent * Add an example of using Redux to get a badge count with React Navigation
- Loading branch information
1 parent
b545cdc
commit 1522f1f
Showing
2 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
example/ReactNavigationReduxConnectedNavigationComponent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import React, { Component } from 'react' | ||
import { connect } from 'react-redux' | ||
import { AppRegistry, StyleSheet, Text, View, Button } from 'react-native' | ||
import { NavigationComponent } from 'react-native-material-bottom-navigation' | ||
import { TabNavigator } from 'react-navigation' | ||
import Icon from 'react-native-vector-icons/MaterialIcons' | ||
|
||
|
||
/** | ||
* Screen for first tab. | ||
* You usually will have this in a separate file. | ||
*/ | ||
class MoviesAndTV extends Component { | ||
static navigationOptions = { | ||
tabBarLabel: "Movies & TV", | ||
tabBarIcon: () => <Icon size={24} name="ondemand-video" color="white" /> | ||
} | ||
|
||
render() { | ||
return <View><Text>Movies & TV</Text></View> | ||
} | ||
} | ||
|
||
/** | ||
* Screen for second tab. | ||
* You usually will have this in a separate file. | ||
*/ | ||
class Music extends Component { | ||
static navigationOptions = { | ||
tabBarLabel: "Music", | ||
tabBarIcon: () => <Icon size={24} name="music-note" color="white" /> | ||
} | ||
|
||
render() { | ||
return <View><Text>Music</Text></View> | ||
} | ||
} | ||
|
||
/** | ||
* Screen for third tab. | ||
* You usually will have this in a separate file. | ||
*/ | ||
class Books extends Component { | ||
static navigationOptions = { | ||
tabBarLabel: "Books", | ||
tabBarIcon: () => <Icon size={24} name="book" color="white" /> | ||
} | ||
|
||
render() { | ||
return <View><Text>Books</Text></View> | ||
} | ||
} | ||
|
||
/** | ||
* Component which wraps NavigationComponent and is passed additional properties which can | ||
* dynamically alter the bottomNavigationOptions. For example, you could use Redux to get | ||
* the unread book count, pass it into this components props, and then change the badge count. | ||
*/ | ||
function UnreadBooksNavigationComponent(props) { | ||
const { unreadBookCount, bottomNavigationOptions, ...rest } = props; | ||
return ( | ||
<NavigationComponent | ||
bottomNavigationOptions={{ | ||
...bottomNavigationOptions, | ||
tabs: { | ||
...bottomNavigationOptions.tabs, | ||
Books: { | ||
...bottomNavigationOptions.tabs.Books, | ||
badgeText: unreadBookCount > 0 ? "" + unreadBookCount : undefined, | ||
} | ||
} | ||
}} | ||
{...rest} | ||
/> | ||
); | ||
} | ||
const ReduxNavigationComponent = connect( | ||
(state) => ({ | ||
unreadBookCount: state.unreadBooks.length | ||
}), | ||
(dispatch) => {} | ||
)(UnreadBooksNavigationComponent); | ||
|
||
/** | ||
* react-navigation's TabNavigator. | ||
*/ | ||
const MyApp = TabNavigator({ | ||
MoviesAndTV: { screen: MoviesAndTV }, | ||
Music: { screen: Music }, | ||
Books: { screen: Books } | ||
}, { | ||
tabBarComponent: ReduxNavigationComponent, | ||
tabBarPosition: 'bottom', | ||
tabBarOptions: { | ||
bottomNavigationOptions: { | ||
labelColor: 'white', | ||
rippleColor: 'white', | ||
tabs: { | ||
MoviesAndTV: { | ||
barBackgroundColor: '#37474F' | ||
}, | ||
Music: { | ||
barBackgroundColor: '#00796B' | ||
}, | ||
Books: { | ||
barBackgroundColor: '#5D4037' | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
AppRegistry.registerComponent('MyApp', () => MyApp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters