简体   繁体   English

反应路由器 this.props.location

[英]react router this.props.location

I need a help with react-router v2+ I have to change class of navbar when route changed for example for route /profile className will be "profile-header" I tried to use this.props.location in navbar component but it shows undefined我需要 react-router v2+ 的帮助我必须在路由更改时更改导航栏的类,例如route /profile className将是"profile-header"我尝试在导航栏组件中使用this.props.location但它显示undefined

Hope your help希望你的帮助

Your navbar component (as you described it in your question) is probably not the route component, right?您的navbar组件(如您在问题中所描述的)可能不是路由组件,对吗? By route component I mean the one that you use in your react-router configuration that is loaded for a specific route.我所说的route component是指您在为特定路由加载的react-router配置中使用的route component

this.props.location is accessible only on such route component , so you need to pass it down to your navbar . this.props.location只能在这样的route component上访问,所以你需要把它传递给你的navbar

Let's take an example:让我们举个例子:

Your router config:您的路由器配置:

<Router>
    <Route path="/" component={App}>
    // ...
</Router

Route component App :路由组件App

class App extends React.Component{
  // ...
  render() {
    return <Navbar location={this.props.location}/>
  }
}

There could be a scenario where you may not have access to props.location to pass to the nav component.在某些情况下,您可能无法访问 props.location 以传递给导航组件。

Take for example - We had a header component in our project which was included in the routing switch to make it available to all routes.举个例子 - 我们的项目中有一个 header 组件,它包含在路由开关中,使其可用于所有路由。

<Switch>
  <Fragment>
    <Header/>
    <Route path='..' component={...}/>
    <Route path='..' component={...}/>
  </Fragment>
</Switch>

In the above scenario there is no way to pass the location data to the Header component.在上述场景中,无法将位置数据传递给 Header 组件。

A better solution would be to us the withRouter HOC when a component is not being rendered by your router.当您的路由器未呈现组件时,更好的解决方案是使用 withRouter HOC。 You will still have access to the router properties history, match and location when you wrap it in the withRouter HOC:当您将它包装在 withRouter HOC 中时,您仍然可以访问路由器属性历史记录、匹配和位置:

import { withRouter } from 'react-router-dom'
.... 
....
export default withRouter(ThisComponent)

react-router v4反应路由器 v4

From documentation :文档

  1. <Route> component property should be used, when you have an existing component.当您有一个现有组件时,应该使用<Route> component属性。 <Route> render property takes an inline function, that returns html. <Route> render属性采用内联函数,该函数返回 html。

  2. A <Route> with no path will always match.没有path <Route>将始终匹配。

Based on this, we can make a <Route> wrapper to any level of your html structure.基于此,我们可以为您的 html 结构的任何级别制作一个<Route>包装器。 It will always be displayed and have access to the location object.它将始终显示并可以访问位置对象。

As per your request, if a user comes to /profile page, the <header> will have profile-header class name.根据您的要求,如果用户访问/profile页面, <header>将具有profile-header类名称。

<Router>
    <Route render={({ location }) =>
        <header className={location.pathname.replace(/\//g, '') + '-header'}>
            // header content...
        </header>

        <div id="content"></div>

        <footer></footer>
    } />
</Router>

I couldn't solve it with the solutions given here and here is what worked for me:我无法使用此处给出的解决方案解决它,这对我有用:

I imported history into my component and assigned history.location.pathname to a variable which I later used for dynamic style manipulation.我将history导入到我的组件中,并将history.location.pathname分配给一个变量,我后来将其用于动态样式操作。

如果您使用预定义的 location.state 值渲染组件,请首先使用 props.location.state 设置您的状态,然后在您的元素中使用您的状态数据。

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

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