Given the following component using useNavigationState, what does currentRoute contain?

4 phương ánCó giải thích
Đề bài
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 án
Lờ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

Xuất hiện trong 1 bộ đề