react-router-dom:从 <BrowserRouter> 组件中获取 props.location

IT技术 reactjs react-router-dom
2021-05-04 05:05:47

我有一个BrowserRouter来自“react-router-dom”v4的简单应用程序我试图location.pathname<BrowserRouter/>组件内部访问该属性,但无济于事:

class App extends Component{
  render(){
    return (
      <BrowserRouter>
        // How do I access this.props.location?
        <div className={(this.props.location.pathnme === "/account") ? "bgnd-black" : "bgnd-white"} >
          <Switch>
            <Route path="/login" component={LoginPage}/>
            <Route path="/success" component={LoginSuccess}/>
            <Route path="/account" component={MyAccount}/>
            ...
            <Route component={Error404}/>
          </Switch>
        </div>
      </BrowserRouter>
    );
  }
}

我知道我可以通过子组件访问应用程序的当前路径位置this.props.location.pathname,但我需要从父组件访问它,就在下面,<BrowserRouter/>以运行与子组件无关的附加逻辑。我怎样才能得到这个位置?

4个回答

您还可以使用withRouterwhich 与将代码放入render参数中的结果类似,并避免使用 "fake" <Route/>

本质上,您将需要知道位置的 JSX 放在自己的组件中,该组件由withRouter. 这提供了组件的位置:

import { withRouter } from 'react-router-dom';

const Content = withRouter(props =>
    <div className={(props.location.pathname === "/account") ? "backg...
    ...
    </div>
);

然后你在你的主路由器部分使用它:

class App extends Component{
    render() {
        return (
            <BrowserRouter>
                <Content/>
                ...

由于react-router v5.1.0您可以使用useLocation.

https://reactrouter.com/web/api/Hooks/uselocation

class App extends Component{
  render(){
    const location = useLocation();

    return (
      <div className={(location.pathname === "/account") ? "bgnd-black" : "bgnd-white"} >
        //...
      </div>
    );
  }
}

// ...
<BrowserRouter>
  <App />
</BrowserRouter>

在挖掘了他们的 GitHub 问题后,我找到了解决方案。我必须渲染一个<Route />内部<BrowserRouter />并将我的应用程序的其余部分作为参数传递到它的render()函数中history在渲染函数中,我可以在history.location.pathname.

class App extends Component{
  render(){
    return (
      <BrowserRouter>

        // We must add a parent <Route> and render its children while passing 'history' as parameter
        <Route path={Paths.reserve} render={(history) => 

          // Within render(), we can find it in history.location.pathname
          <div className={(history.location.pathname === "/account") ? "background-black" : "background-white"} >
            <Switch>
              <Route path="/login" component={LoginPage}/>
              <Route path="/success" component={LoginSuccess}/>
              <Route path="/account" component={MyAccount}/>
              ...
              <Route component={Error404}/>
            </Switch>
          </div>
          }/>
        }} />
      </BrowserRouter>
    );
  }
}

这将history自动更新参数,而无需重新渲染componentDidMount()componentDidUpdate()

你通过这样做实现了你所要求的

import AccessRoute from './AccessRoute'

class App extends Component{
  render(){
    return (
      <BrowserRouter>

  <AccessRoute>    
 <div className={(this.props.location.pathnme === "/account") ? "bgnd-black" : "bgnd-white"} >
          <Switch>
            <Route path="/login" component={LoginPage}/>
            <Route path="/success" component={LoginSuccess}/>
            <Route path="/account" component={MyAccount}/>
            ...
            <Route component={Error404}/>
          </Switch>
        </div>
       </AccessRoute>    
      </BrowserRouter>
    );
  }
}

访问路由.jsx

import React from 'react'
import {withRouter} from 'react-router';
class AccessRoute extends React.Component{
    constructor(props){
        super(props);
    }



 //If you want to find the location on mount use this 

 componentDidMount(){
        console.log("the path name is ",this.props.location.pathname);
    }


 //If you want to find the location on change use this

  componentDidUpdate(prevprops){
    if(this.props.location.pathname!=prevprops.location.pathname){
        console.log("the new path name is ",this.props.location.pathname);
    }

}

    render(){
        return(

            this.props.children

            );
    }
}
export default withRouter(AccessRoute)