捕捉到TypeError:无法读取未定义的属性(reading'pathname')(React)
创始人
2025-01-12 15:30:28
0

这个错误通常在React中的路由器中使用。如果您正在使用React路由器并且遇到此错误,则可能是一个名称错误或您忘记传递props。请确保this.props.location.pathname 位于路由器内部,并检查确保正确地传递了 props。以下是一个示例代码片段,可能会发生此错误:

import React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';

class App extends React.Component {
  render() {
    return (
      
        
); } } const Home = () => { return (

Home

Current pathname: {this.props.location.pathname}

); } const About = () => { return (

About

Current pathname: {this.props.location.pathname}

); } const Contact = () => { return (

Contact

Current pathname: {this.props.location.pathname}

); } const Sitemap = () => { return (

Sitemap

Current pathname: {this.props.location.pathname}

); }

在此示例中,我们尝试从未定义的props访问location.pathname。要解决此问题,我们应该从props对象中访问 location.pathname,并需要将props传递给组件。将Home组件更新如下:

const Home = (props) => {
  return (
    

Home

Current pathname: {props.location.pathname}

); }

或者使用解构:

const Home = ({ location }) => {
  return (
    

Home

Current pathname: {location.pathname}

); }

相关内容

热门资讯

科普!aapoker插件,we... 科普!aapoker插件,wepoker辅助软件视频,安装教程(有挂透明);支持多人共享记分板与复盘...
必知教程!wepoker好友助... 必知教程!wepoker好友助力码,aapoker透视怎么用,AA德州教程(有挂技巧)准备好在aap...
技术分享!德州透视hhpoke... 技术分享!德州透视hhpoker,拱趴大菠萝十三水作弊,黑科技教程(有挂透明);最新版2024是一款...
科技介绍!wepoker黑侠辅... 1、科技介绍!wepoker黑侠辅助器,wepoker有辅助器吗,解密教程(有挂技巧)2、进入游戏-...
总算了解!aapoker插件,... 1、总算了解!aapoker插件,德扑圈透视挂,安装教程(有挂透视)2、进入游戏-大厅左侧-新手福利...
实测教程!hhpoker软件安... 实测教程!hhpoker软件安装包,wepoker软件安装包,德州论坛(有挂技巧);实战中需综合运用...
一分钟了解!pokemmo手机... 一分钟了解!pokemmo手机脚本辅助器,pokerworld破解版下载,教你攻略(有挂软件);实战...
解密关于!wepoker怎么获... 解密关于!wepoker怎么获得好牌,we-poker是什么软件,必胜教程(有挂教程);1分钟了解详...
必备攻略!wepoker私人局... 您好,德普之星辅助工具如何设置这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很...
玩家必看教程!aapoker辅... 玩家必看教程!aapoker辅助器是真的吗,wpk透视辅助方法,玩家教你(有挂方法)关于aapoke...