[英]How to render a component without using a Route element
This is my React component:这是我的反应组件:
const Auth = () => {
useEffect(() => {
document.body.classList.add("bg-default");
return () => {
document.body.classList.remove("bg-default");
}
}, [])
const { isAuthenticated } = useAuth0();
if (isAuthenticated){
return <AdminLayout><Dashboard/></AdminLayout>
} return (
But It's a problem I can't understand.但这是一个我无法理解的问题。
AdminLayout
works in every component but when I return it in the way above I get: TypeError: Cannot read property 'pathname' of undefined AdminLayout
适用于每个组件,但是当我以上述方式返回它时,我得到: TypeError: Cannot read property 'pathname' of undefined
The error is in this:错误在于:
<AdminNavbar {...this.props}
brandText{this.getBrandText(this.props.location.pathname)} />
I think it's about react-router but I don't how to fix it, I'm not very confident with react-router.我认为这是关于 react-router 但我不知道如何修复它,我对 react-router 不是很有信心。
I fixed in this way:我以这种方式修复:
const Auth = () => {
useEffect(() => {
document.body.classList.add("bg-default");
return () => {
document.body.classList.remove("bg-default");
}
}, [])
const { isAuthenticated } = useAuth0();
if (isAuthenticated){
return <AdminLayout><Dashboard/></AdminLayout>
} return (
But It's a problem I can't understand.但这是一个我无法理解的问题。 AdminLayout works in every component but when I return it in the way above I get: TypeError: Cannot read property 'pathname' of undefined
AdminLayout 适用于每个组件,但是当我以上述方式返回它时,我得到: TypeError: Cannot read property 'pathname' of undefined
The error is in this:错误在于:
<AdminNavbar {...this.props}
brandText{this.getBrandText(this.props.location.pathname)} />
It would appear that AdminLayout
expects a prop called location, itself having a prop called pathname.看起来
AdminLayout
需要一个名为 location 的道具,它本身有一个名为 pathname 的道具。 I suppose you could either render it as <AdminLayout location={window.location} />
, or change this.props.location.pathname
to window.location.pathname
, this assuming that you're trying to render different texts depending on where the user is.我想您可以将其呈现为
<AdminLayout location={window.location} />
,或者将this.props.location.pathname
更改为window.location.pathname
,这假设您尝试根据位置呈现不同的文本用户是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.