捕捉到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插件... 透视美元局!aapoker插件(透视)发牌逻辑(一贯存在有挂);1、游戏颠覆性的策略玩法,独创攻略技...
透视安卓版!aapoker怎么... 透视安卓版!aapoker怎么设置提高好牌几率(透视)ai插件(一贯存在有挂)1)aapoker怎么...
透视黑科技!aapoker怎么... 透视黑科技!aapoker怎么提高中牌率(透视)ai插件(好像真的有挂);1、完成aapoker怎么...
透视存在!aapoker怎么提... 透视存在!aapoker怎么提高中牌率(透视)发牌逻辑(切实真的是有挂)小薇(透视辅助)致您一封信;...
透视规律!aapoker俱乐部... 透视规律!aapoker俱乐部靠谱吗(透视)脚本(真是存在有挂)1、透视规律!aapoker俱乐部靠...
透视代打!aapoker怎么选... 透视代打!aapoker怎么选牌(透视)万能辅助器(都是真的是有挂)一、aapoker怎么选牌AI软...
透视模拟器!aapoker怎么... 透视模拟器!aapoker怎么拿好牌(透视)免费透视脚本(果然真的有挂)1、操作简单,无需注册,只需...
透视有挂!aapoker辅助插... 透视有挂!aapoker辅助插件工具(透视)透视脚本(竟然存在有挂);1、下载好aapoker辅助插...
透视肯定!aapoker脚本怎... 透视肯定!aapoker脚本怎么用(透视)插件下载(竟然有挂)aapoker脚本怎么用辅助器中分为三...
透视中牌率!aapoker辅助... 透视中牌率!aapoker辅助器怎么用(透视)透视软件(确实有挂)运aapoker辅助器怎么用辅助工...