我有一个本机应用程序,每当特定屏幕的状态变为“聚焦”时,我都希望执行一个功能。根据文档(link),我应该使用useFocusEffect
,这是我的代码:
import {useFocusEffect} from '@react-navigation/native'
export default function ProfileScreen(props) {
const {loading} = props.store
const [deviceId, setDeviceId] = React.useState(null)
const [questionIndex, setQuestionIndex] = React.useState(0)
console.log(useFocusEffect)
useFocusEffect(
React.useCallback(() => {
console.log('here')
//AsyncStorage.getItem('deviceId', (err, result) => {
// if (result !== null) {
// setDeviceId(result)
// props.dispatch({type: "STOP_LOADER"})
// }
//})
}, [])
)
问题是,当应用加载时,出现此错误:
TypeError: (0, _native.useFocusEffect) is not a function.
我正在使用的版本是这个版本:"@react-navigation/native": "^3.6.2",
,我该如何解决?
版本4提供了类似useFocusEffect的钩子。如果无法升级到版本4,请查看版本3的文档:https://reactnavigation.org/docs/en/3.x/getting-started.html