【问题标题】:How can i redirect to other screens from App.js?如何从 App.js 重定向到其他屏幕?
【发布时间】:2021-09-14 01:06:16
【问题描述】:

当在 App.js 中收到通知时,我正尝试重定向到另一个屏幕。我正在使用 useNavigation() 但它不起作用。

有没有办法重定向到另一个屏幕或以其他方式使用通知?

export default function App() {
  function UserScreen() {  
    const navigation = useRef(useNavigation());  
    navigation.navigate('Chat');
  }


  useEffect(() => {
    const backgroundSubscription = Notifications.addNotificationResponseReceivedListener(
      (response) => {
        switch (response.request?.content.data.type) {
          case 'chat':
            return UserScreen()
          default:
            return console.log('Entro al default')
        }
      }
    );

    const foregroundSubscription = Notifications.addNotificationReceivedListener(
      (notification) => {
        switch (notification.request?.content.data.type) {
          case 'chat':
            return UserScreen()
          default:
            return console.log('Entro al default')
        }
      }
    );

    return () => {
      backgroundSubscription.remove();
      foregroundSubscription.remove();
    };
  }, []);

  return (
    <Provider store={store}>
      <SparkitNavigator />
    </Provider>

  );
}

【问题讨论】:

  • 试试const navigation = useNavigation();让我知道
  • 移动 const navigation 到 App() 的顶部,钩子不应该在嵌套函数中使用

标签: reactjs react-native react-hooks react-navigation expo-notifications


【解决方案1】:

怎么样:

export default function App() {
  const navigation = useNavigation(); 
  
  function UserScreen() {  
   navigation.navigate('Chat');
  }
  // rest code
}

确保应用和聊天已在导航器堆栈中注册

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 2019-11-21
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多