简体   繁体   中英

How to get Route name from current URL

I am working on small project in React. I want to get Route name from the current URL in ReactJS.

I am currently using React-Router 3.2.0 and I want to solve this problem in React-router (3.2.0 ).

Following on from the comments on the question, and the chat. You can't access location prop because the component you're on is not rendered by a Route component. Easy way to solve this, at the top of your file, include the import:

import { withRouter } from 'react-router';

then, where you're exporting your component, wrap it in withRouter(... component here ...) So for example, your Routes component export will look like:

export default withRouter(connect(
    mapStateToProps,
    {}
    )(Routes))

You'll then be able to access this.props.location which will have a value, pathname ( this.props.location.pathname ) which will be the full route, minus the basePath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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