使用USENAVITAIGE的React通过无法读取NULL的属性

发布于 2025-02-09 08:04:32 字数 597 浏览 2 评论 0原文

我正在使用USENAVITAITE将数据传递在组件之间。

import { useNavigate } from "react-router-dom";

function Categories() {
const navigate = useNavigate();
.
.
onClick={() => navigate("/books", {state:{ name: categorie.name }})}

然后,我从第二个组件中依靠其工作正常的数据

import {useLocation} from "react-router-dom";
function Books() {
const location = useLocation();
let currentcategory = location.state.name;

,但是如果我尝试使用URL https:// localhost/books直接访问

此错误,我会收到此错误

Uncaught TypeError: Cannot read properties of null (reading 'name')

I'm using useNavigate to pass data between components.

import { useNavigate } from "react-router-dom";

function Categories() {
const navigate = useNavigate();
.
.
onClick={() => navigate("/books", {state:{ name: categorie.name }})}

Then I resive the data from second component

import {useLocation} from "react-router-dom";
function Books() {
const location = useLocation();
let currentcategory = location.state.name;

Its works fine but If I try to access directly using URL https://localhost/books

I got this error

Uncaught TypeError: Cannot read properties of null (reading 'name')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ可否回来 2025-02-16 08:04:32

问题

问题是,如果您从提供状态的组件过渡,则仅存在路由/链接/导航状态 。如果您在地址栏中手动输入接收路由的URL路径,则该状态丢失或未定义/空。

换句话说,location.state是未定义的/null,并且在进一步访问它以获取location.state.state.name时会抛出错误。

解决方案

使用null-Check/Guard-Clause或可选的链式操作员来防御潜在的无效访问,或者为破坏性提供后备价值。

  • null-Check/guard-clause

      const {state} = uselocation();
    const CurrentCategory = state&& state.name;
     
  • 可选的链接操作员

      const {state} = uselocation();
    const current category = state?.name;
     
  • 破坏的后备对象

      const {state} = uselocation();
    const {name:currentCategory} =状态|| {};
     

Issue

The issue is that the route/link/navigate state only exists if you transition from the component providing the state. If you manually enter the receiving route's URL path in the address bar, this state is missing, or undefined/null.

In other words, location.state is undefined/null, and the error is thrown when accessing further into it to get location.state.name.

Solution

Use a null-check/guard-clause or the Optional Chaining operator to defend against the potentially null access, or provide a fallback value to destructure from.

  • Null-check/guard-clause

    const { state } = useLocation();
    const currentCategory = state && state.name;
    
  • Optional Chaining operator

    const { state } = useLocation();
    const currentCategory = state?.name;
    
  • Fallback object to destructure from

    const { state } = useLocation();
    const { name: currentCategory } = state || {};
    
幼儿园老大 2025-02-16 08:04:32

你需要破坏它

const {state} = useLocation();
console.log(state)

you need to destructure it

const {state} = useLocation();
console.log(state)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文