Consider the following component using useIsFocused and useEffect. What will be the behavior
4 phương ánCó giải thíchKhó
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á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 useIsFocused và useEffect trong một thành phần, chúng ta cần hiểu cách hoạt động của chúng. useIsFocused sẽ trả về một giá trị boolean cho biết thành phần đang được tập trung vào hay không. useEffect sẽ được gọi khi giá trị của useIsFocused thay đổi. Trong ví dụ này, useEffect sẽ được gọi khi thành phần được tập trung vào. Nếu thành phần ban đầu không được tập trung vào và sau đó được tập trung vào, useEffect sẽ được gọi và in ra 'ProfileScreen is now focused'. Điều này xảy ra vì useEffect được gọi khi giá trị của useIsFocused thay đổi, và khi thành phần được tập trung vào, giá trị của useIsFocused sẽ trở thành true. Vì vậy, đáp án đúng là 'It will log 'ProfileScreen is now focused' every time the component gains focus'.
Cùng chương 5
In React Navigation, which hook should you use to get access to route parameters inside a functional component?Có lời giảiCâu hỏiWhich of the following hooks can you use to perform an action only when the screen is first mounted in React Navigation?Có lời giảiCâu hỏiWhich hook would you use to get access to the navigation object within a functional component in React Navigation?Có lời giảiCâu hỏiCả 13 câu của chủ đề13 câu