Web / React native Interview questions
Explain useFocusEffect hook in React-native.
We may want to run side-effects when a screen is focused such as adding an event listener, fetching data, updating document title, etc.
useFocusEffect executes every time when the screen is focused. The useFocusEffect is analogous to React's useEffect hook. The only difference is that it only runs if the screen is currently focused.
import { useFocusEffect } from '@react-navigation/native'; function Profile({ userId }) { const [user, setUser] = React.useState(null); useFocusEffect( React.useCallback(() => { const unsubscribe = API.subscribe(userId, user => setUser(user)); return () => unsubscribe(); }, [userId]) ); return <ProfileContent user={user} />; }
More Related questions...