捕捉到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、德普之星辅助...
经调查!steampokerm... 经调查!steampokermaster辅助(透视)总是存在有辅助修改器(有挂详情)-哔哩哔哩1、s...
透视系统!hhpoker德州有... 透视系统!hhpoker德州有挂吗(透视)确实有辅助下载(有挂规律)-哔哩哔哩1、在hhpoker德...
透视工具!hhpoker软件可... 透视工具!hhpoker软件可以玩吗(透视)切实有辅助软件(有挂规律)-哔哩哔哩1、玩家可以在hhp...
经核实!xpoker辅助(透视... 经核实!xpoker辅助(透视)一贯是真的辅助安装(有挂秘诀)-哔哩哔哩亲,关键说明,xpoker辅...
据监测!哈糖大菠萝攻略(透视)... 据监测!哈糖大菠萝攻略(透视)确实真的是有辅助工具(有挂助手)-哔哩哔哩1、下载好哈糖大菠萝攻略正确...
透视透视!hhpoker的辅助... 透视透视!hhpoker的辅助是真的吗(透视)真是存在有辅助神器(有挂攻略)-哔哩哔哩1、让任何用户...
针对!wpk透视插件(透视)本... 针对!wpk透视插件(透视)本来真的有辅助修改器(有挂解密)-哔哩哔哩1、wpk透视插件辅助软件下载...
出乎意料的是!wejoker开... 出乎意料的是!wejoker开挂(透视)原来真的是有辅助神器(有挂透视)-哔哩哔哩1、wejoker...
有消息称!hhpoker的辅助... 有消息称!hhpoker的辅助是真的吗(透视)竟然真的有辅助app(发现有挂)-哔哩哔哩在进入hhp...