捕捉到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}

); }

相关内容

热门资讯

五分钟诀窍!来玩德州破解器(透... 五分钟诀窍!来玩德州破解器(透视)其实真的有辅助辅助(哔哩哔哩)1、五分钟诀窍!来玩德州破解器(透视...
第九分钟诀窍!wepoker开... 第九分钟诀窍!wepoker开辅助能查到吗(透视)切实有辅助透视(哔哩哔哩)1、wepoker开辅助...
第8分钟技法!佛手在线大菠萝为... 第8分钟技法!佛手在线大菠萝为什么都输(透视)切实是有辅助教程(哔哩哔哩)1)佛手在线大菠萝为什么都...
第六分钟绝活!sohoopok... 第六分钟绝活!sohoopoker辅助(透视)好像是真的有辅助脚本(哔哩哔哩)1、下载好sohoop...
9分钟要领!wepoker破解... 您好,wepoker破解游戏盒子这款游戏可以开挂的,确实是有挂的,需要了解加去威信【48527505...
第3分钟攻略!wpk辅助插件(... 第3分钟攻略!wpk辅助插件(透视)一贯是真的有辅助透视(哔哩哔哩)在进入wpk辅助插件软件靠谱后,...
六分钟讲义!we-poker辅... 六分钟讲义!we-poker辅助器(透视)竟然存在有辅助开挂(哔哩哔哩)1、起透看视 we-poke...
第九分钟攻略!hhpoker有... 第九分钟攻略!hhpoker有后台操控吗(透视)一贯真的有辅助下载(哔哩哔哩)1、hhpoker有后...
十分钟讲义!wepoker破解... 十分钟讲义!wepoker破解器(透视)真是真的是有辅助下载(哔哩哔哩)所有人都在同一条线上,像星星...
七分钟资料!hhpoker辅助... 七分钟资料!hhpoker辅助器(透视)真是是真的有辅助透视(哔哩哔哩)小薇(辅助器软件下载)致您一...