What is the main advantage of hooks in React Navigation 5 compared to previous versions?
4 phương ánCó giải thích
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng gồm hai ý: "Allows easier use in functional components." và "Reduces or eliminates the dependency on Higher-Order Components (HOCs).". React Navigation 5 thêm các hook như useNavigation, useRoute, useFocusEffect, useIsFocused; component hàm ở bất kỳ tầng nào gọi hook là lấy được đối tượng navigation hay route, không cần nhận qua prop. Trước đó, component con muốn điều hướng phải được bọc bằng HOC (Higher-Order Component, hàm nhận component và trả về component mới) như withNavigation hay withNavigationFocus, làm cây component rối và khó khai báo kiểu. "Completely eliminates the need for a navigation container." sai vì từ bản 5 trở đi ứng dụng vẫn phải bọc navigator trong NavigationContainer; hook không thay thế nó. "Requires the use of class components." sai vì hook chỉ dùng được trong component hàm, còn class component vẫn nhận navigation qua prop như cũ.
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ả 24 câu của chủ đề24 câu