简体   繁体   English

如何在不使用 Route 元素的情况下渲染组件

[英]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.

相关问题 如何使用GatsbyJS根据路线渲染组件? - How to render a component according to the route using GatsbyJS? ReactRouter将渲染组件路由到容器元素 - ReactRouter route render component to container element 如何根据路由在组件内渲染组件 - How to render component inside a component based on route 如何在每条路线中呈现相同的组件 - how to render same component in every route 如何使用 React 在组件渲染中禁用元素的动画? - How do I disable animation of a element in a component render using React? 如何 1). 放一个 div,2)。 渲染一个内部没有路由的组件<routes>在 v6 React 中使用 React Router?</routes> - How to 1). put a div , 2). render a component without Route inside <Routes> with React Router in v6 React? 如何在不显示根组件的情况下路由组件 - How to route a component without displaying root component 当使用Route render prop而不是Route component prop时,如何访问react-router-dom 4中的url args? - How do you access url args in react-router-dom 4 when using Route render prop instead of Route component prop? 在不使用路由器的情况下渲染新组件 - Render a new Component without using router 如何在功能组件中呈现嵌套元素? - How to render nested element in functional component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM