[英]TypeError: Property is undefined | React
我是React的新手,所以這個問題可能很基礎,但是我想尋求幫助。
由於no-restricted-globals
,所有突然的Eslint開始對我喊叫,所以我開始修復這些錯誤。 但是,我現在遇到了這個經典錯誤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>
)
}
如果有人解釋了為什么未定義location
,我將不勝感激。 感謝您閱讀大家!
嘗試使用此:
import React from 'react' export default props => { const url = ['#'].concat(props.to).join('/') const location= this.props.location?this.props.location:[] return ( {props.children} ) }
希望這可以正常運行!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.