在 react-router v4 中获取路径参数 [英] Get path params in react-router v4
问题描述
我正在尝试通过我的应用程序构建路由器链接,
在这个场景中,我有三个文件.
App.js
Book.js
DetailedView.js
我在 Book 内部建立了一个 <Link>
仅在悬停(在书籍封面上)时出现
{this.state.isHovered ?(<链接到={`/details/${this.props.book.industryIdentifiers[1].identifier}`}><div className="hover-box"></div></Link>) : ( <div/> )}
这将带我到/details/12345(isbn10 编号)
我很难理解的是如何例如setState({iPressedThisBook})
当按下 或者我可以使用
/12345
之后的部分来创建一个过滤器>
因为在 App
中,Route
将被连接为...
(<BookDetailedViewbookStateUpdated = {this.bookStateUpdated}书 = {this.state.books}/>)}/>
我稍后想要获取 :id
以便我在我的 this.props.book.find(:id)
中创建一个 this.props.book.find(:id)
代码>
为了在你的组件中接收路径参数,你需要首先将你的组件与 react 中的
以便您可以访问路由器道具并从 withRouter
HOC 连接起来-routermatch
道具中获取路径 params
作为 this.props.match.params.id
示例代码:
import {withRouter} from 'react-router';class BookDetailedView 扩展 React.Component {使成为() {var id = this.props.match.params.id}}导出默认 withRouter(BookDetailedView) ;
或者简单地在路由中使用渲染道具传递它
(<BookDetailedViewbookStateUpdated = {this.bookStateUpdated}书 = {this.state.books}id={match.params.id}/>)}/>
来自 match
匹配
匹配对象包含有关
如何匹配的信息网址.match
对象包含以下属性:
- params - (object) 从对应于路径动态段的 URL 解析出的键/值对
- isExact - (布尔值)如果整个 URL 匹配(无尾随字符)为真
- path - (字符串)用于匹配的路径模式.用于构建嵌套 s
- url -(字符串)URL 的匹配部分.用于构建嵌套 s
您可以在不同的地方访问匹配对象:
- 将组件路由为
this.props.match
- 路由渲染为 ({ match }) => ()
- 将子级路由为 ({ match }) => ()
- withRouter as this.props.match
- matchPath 作为返回值
如果 Route 没有路径,因此总是匹配,您将获得最接近的父匹配.withRouter 也一样
I'm trying to build a router link through my application,
In this scenario, I have three files.
App.js
Book.js
DetailedView.js
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 /> )}
This will take me to a /details/12345 (isbn10 number)
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
Because in App
the Route
will be hooked up as...
<Route path="/details/:id" render={() => (
<BookDetailedView
bookStateUpdated = {this.bookStateUpdated}
book = {this.state.books}
/>
)}/>
I, later on, want to grab the :id
so that I make for example a this.props.book.find(:id)
inside of my <BookDetailedView>
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
Sample Code:
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}
/>
)}/>
From the React Documentation of match
match
A match object contains information about how a
<Route path>
matched the URL.match
objects contain the following properties:
- 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:
- Route component as
this.props.match
- Route render as ({ match }) => ()
- Route children as ({ match }) => ()
- withRouter as this.props.match
- matchPath as the return value
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屋!