reactjs – 从标题中的按钮导航到不同的屏幕

前端之家收集整理的这篇文章主要介绍了reactjs – 从标题中的按钮导航到不同的屏幕前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用新的 React-navigation从反应本地。我有导航如下:

StackNavigator:

> TabNavigator // HomeNavigation
> TabNavigator // NotificationNavigation

全部代码

  1. const MainNavigation = StackNavigator({
  2. Home: {
  3. screen: HomeNavigation,},Notification: {
  4. screen: NotificationNavigation,}
  5. });
  6.  
  7. const HomeNavigation = TabNavigator({
  8. AllPost: {
  9. screen: All,ArticlePost: {
  10. screen: Article,BusinessPost: {
  11. screen: Job,});
  12.  
  13. HomeNavigation.navigationOptions = {
  14. title: 'Home',header: {
  15. right: <SearchNotification/>
  16. },};
  17.  
  18. class SearchNotification extends React.Component {
  19. goToNotification = () => {
  20. this.props.navigate('Notification');
  21. };
  22.  
  23. render() {
  24. return (
  25. <View style={styles.container}>
  26. <TouchableOpacity>
  27. <Icon name="md-search" style={styles.Icon}/>
  28. </TouchableOpacity>
  29. <TouchableOpacity style={styles.notificationWrapper} onPress={this.goToNotification}>
  30. <Icon name="md-notifications" style={styles.Icon}/>
  31. <Text style={styles.number}>3</Text>
  32. </TouchableOpacity>
  33. </View>
  34. )
  35. }
  36. }
  37.  
  38. const NotificationNavigation = TabNavigator({
  39. Comment: {
  40. screen: NotificationComment,Follow: {
  41. screen: NotificationFollow,}
  42. });

HomeNavigation有一个标题标题具有SearchNotification的正确组件。 SearchNotification有一个图标,按照我想去“通知导航”。

但是,如果以这种方式更改HomeNavigation的标题,则SearchNotification不会显示标题中。

  1. HomeNavigation.navigationOptions = {
  2. title: 'Home',header: {
  3. tintColor: 'white',style: {
  4. backgroundColor: '#2ec76e',right: ({navigate}) => <SearchNotification navigate={navigate}/>
  5. },};

如何从标题中的按钮导航到不同的屏幕?

所以问题是(我想),在navigationOptions中,而不是使用导航,我不得不使用导航,并将其作为道具传递给孩子(即SearchNotification)。

所以最终的代码如下所示:

  1. HomeNavigation.navigationOptions = {
  2. title: 'Home',header: ({navigate}) => ({
  3. right: (
  4. <SearchNotification navigate={navigate}/>
  5. ),}),};

在SearchNotification组件中:

  1. export default class SearchNotification extends React.Component {
  2. goToNotification = () => {
  3. this.props.navigate('Notification');
  4. };
  5.  
  6. render() {
  7. return (
  8. <View style={styles.container}>
  9. <TouchableOpacity>
  10. <Icon name="md-search" style={styles.Icon}/>
  11. </TouchableOpacity>
  12. <TouchableOpacity style={styles.notificationWrapper}
  13. onPress={() => this.props.navigate('Notification')}>
  14. <Icon name="md-notifications" style={styles.Icon}/>
  15. <Text style={styles.number}>3</Text>
  16. </TouchableOpacity>
  17. </View>
  18. )
  19. }
  20. }

猜你在找的React相关文章