Given the following component using useNavigationState, what does currentRoute contain?
4 phương ánCó giải thích
import { useNavigationState } from '@react-navigation/native';
function CurrentRouteComponent() {
const currentRoute = useNavigationState(state => state.routes[state.index].name);
return (
<Text>Current Route: {currentRoute}</Text>
);
}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: "The name of the current route". useNavigationState nhận một hàm selector, gọi nó với state của navigator gần nhất và trả về kết quả, đồng thời render lại component khi kết quả đổi. Lần theo mã: state.routes là mảng các route trong navigator, state.index là vị trí route đang hiển thị, nên state.routes[state.index] là đối tượng route hiện tại, và thêm .name lấy ra tên của nó, ví dụ "Home" hay "Profile"; component in chuỗi đó trong Text. "The index of the current route in the navigation stack" sai vì đó là state.index, mã chỉ dùng index để truy cập phần tử chứ không trả về nó. "The parameters of the current route" sai vì muốn lấy tham số phải đọc .params thay vì .name. "The state object of the current route" sai vì selector đi thêm một bước tới .name, trả về chuỗi chứ không trả về cả đối tượng route.
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