useFocusEffect

Similar to React useEffect, but only runs when the current route is focused. This is directly based off React Navigation's useFocusEffect.

You should pass it a function that is wrapped in a useCallback to avoid running it every render.

Example:

import { useFocusEffect } from '@react-navigation/native'
import { useCallback } from 'react'
function Profile({ userId }) {
const [user, setUser] = React.useState(null)
useFocusEffect(
useCallback(() => {
const unsubscribe = API.subscribe(userId, (user) => setUser(user))
return () => unsubscribe();
}, [userId])
)
return <ProfileContent user={user} />
}

Edit this page on GitHub.