【问题标题】:NavigationEvents in react-native navigation-6react-native navigation-6 中的 NavigationEvents
【发布时间】:2022-01-15 18:34:03
【问题描述】:

您好,我正在尝试实现导航,但我不断收到错误

Invariant Violation: withNavigation can only be used on a view hierarchy of a navigator. The wrapped component is unable to get access to navigation from props or context.

这是我的代码,适用于以前版本的 react-navigation

import { View, StyleSheet, Text } from "react-native"
import { NavigationEvents } from 'react-navigation';


const Home = (props) =>{
   
    return (<View>
        <NavigationEvents
                onWillFocus={()=>{
                  console.log("onwillfocus")
                }}
                onDidFocus={() =>{
                   console.log("onDidFocus")
                  }}
            />
        <Text>Hello world</Text>
    </View>)
}

export default Home

【问题讨论】:

    标签: react-native events navigation focus


    【解决方案1】:

    如果您需要检测屏幕是否聚焦,React Navigation 6 会公开一个简单的 useIsFocused 钩子 - https://reactnavigation.org/docs/use-is-focused/

    【讨论】:

      猜你喜欢
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-25
      • 2017-10-11
      • 2019-06-25
      • 1970-01-01
      • 2018-10-27
      相关资源
      最近更新 更多