首页 > 解决方案 > 如何使用useRoute在app.js中显示当前屏幕名称?

问题描述

我正在尝试在 app.js 中获取当前屏幕名称,我在通知处理程序中使用 useRoute 但未显示。

Notifications.setNotificationHandler({  
 handleNotification: (n) => {    
  const { keeperId, type } = n?.request?.content?.data ?? {};     
  const route = useRoute();      
  console.log('route name');      
  console.log(route.name)      
  const showNotification = keeperId === store.getState().chat.chat.keeperUserId;        
  return new Promise((resolve) =>
    resolve({
      shouldShowAlert: showNotification,
      shouldPlaySound: false,
      shouldSetBadge: false,
    }),
  );
 },
});

标签: reactjsreact-nativereact-hooksnavigationreact-navigation

解决方案


我解决了这个问题

export const navigationRef = React.createRef();


navigationRef.current.getCurrentRoute().name

推荐阅读