Consider the following code
4 phương ánCó giải thíchDễ
using useIsFocused: import React, {
useEffect
}from 'react'; import { Text } from 'react-native'; import { useIsFocused } from '@react-navigation/native'; function HomeScreen() { const isFocused = useIsFocused(); useEffect(() => {
if (isFocused) {
console.log('HomeScreen is focused');
}else { console.log('HomeScreen is not focused'); } }, [isFocused]); return <Text>Home Screen</Text>; } What will be logged if the component is initially focused and then loses focus?
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Khi sử dụng hàm `useIsFocused` trong React Native Navigation, chúng ta có thể theo dõi xem màn hình hiện tại có đang được tập trung hay không. Trong ví dụ trên, chúng ta có một hàm `HomeScreen` sử dụng `useIsFocused` để kiểm tra xem màn hình đang được tập trung hay không. Hàm `useEffect` được sử dụng để thực hiện hành động khi màn hình được tập trung hoặc mất tập trung. Nếu màn hình được tập trung, sẽ in ra 'HomeScreen is focused' vào console, còn nếu mất tập trung, sẽ in ra 'HomeScreen is not focused'. Khi component được khởi tạo và ban đầu được tập trung, sẽ in ra 'HomeScreen is focused'. Sau đó, khi màn hình mất tập trung, sẽ in ra 'HomeScreen is not focused'. Vì vậy, kết quả sẽ là 'HomeScreen is focused' ban đầu, sau đó là 'HomeScreen is not focused'.
Cùng chương 5
Consider the following component using useIsFocused and useEffect. What will be the behavior if the component is initially unfocused and then gets focused? import React, { useEffect } from 'react'; import { Text } from 'react-native'; import { useIsFocused } from '@react-navigation/native'; function ProfileScreen() { const isFocused = useIsFocused(); useEffect(() => { if (isFocused) { console.log('ProfileScreen is now focused'); } }, [isFocused]); return <Text>Profile Screen</Text>; }Có lời giảiCâu hỏiWith hook in React Navigation, what will happen if you call useFocusEffect inside a functional component but provide an empty dependency array?Có lời giảiCâu hỏiIn React Navigation, which hook should you use to get access to route parameters inside a functional component?Có lời giảiCâu hỏiCả 13 câu của chủ đề13 câu