What mechanism does React Router use to pass dynamic data (like an id) from the URL to a component?
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: "Route parameters, marked by a colon (e.g., :id) in the path, which are then passed to the component.". React Router dùng tham số tuyến (route parameter): trong path, đoạn có dấu hai chấm như /products/:id là chỗ trống động, URL /products/7 khớp tuyến đó và giá trị 7 được đưa vào component, đọc qua useParams() ở v6. "Query parameters, accessed via location.search." là tham số truy vấn sau dấu hỏi như ?id=7, đọc qua location.search hoặc useSearchParams; nó vẫn chở được dữ liệu nhưng không phải cơ chế tham số động khai báo trong path mà câu hỏi nói tới. "State variables, declared with useState()" sai vì useState tạo state nội bộ của component, không lấy giá trị từ URL. "Context API, provided globally to all components" sai vì Context API chia sẻ dữ liệu qua cây component, không đọc gì từ URL.
Cùng phần 7
How would you navigate programmatically to a route with a parameter using useNavigate in React Router?Có lời giảiCâu hỏiHow do you declare a route in React Router?Có lời giảiCâu hỏiIn React Router, how do you define a nested route structure within a parent component?Có lời giảiCâu hỏiCả 39 câu của chủ đề39 câu