简体   繁体   English

TypeError:属性未定义 应对

[英]TypeError: Property is undefined | React

I am new to React so this questions is probably very basic, but I'd like some help. 我是React的新手,所以这个问题可能很基础,但是我想寻求帮助。

All of the sudden Eslint started shouting at me because of no-restricted-globals , so I began to fix those errors. 由于no-restricted-globals ,所有突然的Eslint开始对我喊叫,所以我开始修复这些错误。 However, I got this classical error just now TypeError: Cannot read property 'location' of undefined , and here's the code: 但是,我现在遇到了这个经典错误TypeError: Cannot read property 'location' of undefined ,这是代码:

import React from 'react'

export default props => {
  const url = ['#'].concat(props.to).join('/')
  const { location } = this.props
  return (
    <a
      href={url}
      onClick={props.onClick}
      className={location.hash.indexOf(url) === 0 ? 'active' : undefined}
    >
      {props.children}
    </a>
  )
}

I would really appreciate if someone explained why location is undefined, I just can't get that. 如果有人解释了为什么未定义location ,我将不胜感激。 Thanks for reading everyone! 感谢您阅读大家!

Try to use this: 尝试使用此:

import React from 'react'

    export default props => {
      const url = ['#'].concat(props.to).join('/')
      const location= this.props.location?this.props.location:[]
      return (
        
          {props.children}
        
      )
    }

Hope This might run properly! 希望这可以正常运行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM