在react-router v4中获取路径参数 [英] Get path params in react-router v4
问题描述
我正在尝试通过我的应用程序构建路由器链接,
I'm trying to build a router link through my application,
在这种情况下,我有三个文件。
In this scenario, I have three files.
App.js
Book.js
DetailedView.js
我在Book里面建立了一个< Link>
,它只在悬停时出现(在书籍封面上)
I have inside of Book built up a <Link>
that only appears when hovered ( over a book cover )
{this.state.isHovered ? (
<Link to={`/details/${this.props.book.industryIdentifiers[1].identifier}`}>
<div className="hover-box"></div>
</Link>) : ( <div /> )}
这将带我到/ details / 12345(isbn10号码)
This will take me to a /details/12345 (isbn10 number)
我很难理解的是如何例如
setState({iPressedThisBook})
按< Link>
或者如果我可以在<$ c之后使用该部分$ c> / 12345 创建过滤器
The thing I have a hard time to understand is how to for example
setState({iPressedThisBook})
when pressing <Link>
or if i can use the part after /12345
to create like a filter
因为在 App
中路线
将被连接为......
Because in App
the Route
will be hooked up as...
<Route path="/details/:id" render={() => (
<BookDetailedView
bookStateUpdated = {this.bookStateUpdated}
book = {this.state.books}
/>
)}/>
我稍后想要获取:id
所以我在我的< BookDetailedView> $里面做了一个
this.props.book.find(:id)
c $ c>
I, later on, want to grab the :id
so that I make for example a this.props.book.find(:id)
inside of my <BookDetailedView>
推荐答案
要在您的组件中接收路径参数,您需要先用<$连接组件c $ c> withRouter 来自 react-router
的HOC,以便您可以访问路径道具并获取路径 params
从匹配
道具 this.props.match.params.id
In order to receive the path param in you component, you need to first connect your component with withRouter
HOC from react-router
so that you can access the Router props and get the path params
from the match
props as this.props.match.params.id
示例代码:
import {withRouter} from 'react-router';
class BookDetailedView extends React.Component {
render() {
var id = this.props.match.params.id
}
}
export default withRouter(BookDetailedView) ;
或者只是将其与路线中的渲染道具一起传递为
or simply passing it with render prop in route as
<Route path="/details/:id" render={({match}) => (
<BookDetailedView
bookStateUpdated = {this.bookStateUpdated}
book = {this.state.books}
id={match.params.id}
/>
)}/>
来自 匹配
匹配
匹配对象包含有关<路径路径> $ c $的信息c>匹配
的URL。 匹配
对象包含以下属性:
A match object contains information about how a <Route path>
matched
the URL. match
objects contain the following properties:
- params - (对象)键/值从与路径的动态段对应的URL解析的对
- isExact - (布尔值)如果整个URL匹配(没有尾随字符),则为true
- path - (字符串)用于匹配的路径模式。用于构建嵌套的s
- url - (字符串)URL的匹配部分。用于构建嵌套的s
- params - (object) Key/value pairs parsed from the URL corresponding to the dynamic segments of the path
- isExact - (boolean) true if the entire URL was matched (no trailing characters)
- path - (string) The path pattern used to match. Useful for building nested s
- url - (string) The matched portion of the URL. Useful for building nested s
您将在不同的地方拥有访问匹配对象:
You’ll have access match objects in various places:
- 路由组件为
this.props.match
- 路线渲染为({match} )=>()
- 将子路由为({match})=>()
- withRouter as this.props.match
- matchPath作为返回值
- Route component as
this.props.match
- Route render as ({ match }) => ()
- Route children as ({ match }) => ()
- withRouter as this.props.match
- matchPath as the return value
如果Route没有路径,因此总是匹配,那么你'll
获得最接近的父匹配。同样适用于路由器
If a Route does not have a path, and therefore always matches, you’ll get the closest parent match. Same goes for withRouter
这篇关于在react-router v4中获取路径参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!