如何访问当前屏幕状态并从根组件导航

时间:2018-10-26 09:44:08

标签: javascript react-native react-navigation

在根组件中处理通知时,我希望能够检查当时哪个屏幕处于活动状态,并根据通知的数据导航到特定屏幕。我曾尝试从Layout对象导航,但似乎找不到找到导航的方法。

根组件:

import React from 'react';
import {createRootNavigator} from './router';
import Expo , {Permissions, Notifications } from 'expo';
import {Alert, Platform} from 'react-native';

const Layout = createRootNavigator();

export default class App extends React.Component {
  state = {
      notification: {},
      isSplashReady: false,
      isAppReady: false,
  };
  componentDidMount() {

    this._notificationSubscription = Notifications.addListener(this._handleNotification);

  }

  removeNotifications(){
    if(Platform.OS === 'ios'){
        Expo.Notifications.setBadgeNumberAsync(0);
    }
    else{
        Expo.Notifications.dismissAllNotificationsAsync();
    }
  }

  _handleNotification = (notification) => {
    this.setState({notification: notification});

    Alert.alert(
      this.state.notification.data.title,
      this.state.notification.data.body,
      [
        {text: 'Ok', onPress: () => this.removeNotifications(), style: 'cancel'},
      ],
      { cancelable: true }
    )
  };
    render() {      
      return (
           <Layout />
    );
  }
}

0 个答案:

没有答案