react-native navigation-6 中的 NavigationEvents

NavigationEvents in react-native navigation-6

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

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 Navigation 6 公开了一个简单的 useIsFocused 挂钩 - https://reactnavigation.org/docs/use-is-focused/