react-router传参
Posted ipoodle
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react-router传参相关的知识,希望对你有一定的参考价值。
1.配置中加id
//容器 class Wrapper extends React.Component { constructor(props){ super (props) } render() { return ( <div> <Link to=‘/a/123‘>组件A</Link> <br/> <Link to=‘/b‘>组件B</Link> {this.props.children} </div> ) } }
2.获取参数使用:
this.props.match.params.id
组件a接收参数的代码:
class A extends React.Component { constructor (props){ super (props) } render(){ return ( <div> component A 参数是:{this.props.match.params.id} </div> ) } }
3.页面显示
4.完整代码
import React from ‘react‘; import ReactDOM from ‘react-dom‘; import {BrowserRouter as Router,Route,Link} from ‘react-router-dom‘; class A extends React.Component { constructor (props){ super (props) } render(){ return ( <div> component A 参数是:{this.props.match.params.id} </div> ) } } class B extends React.Component { constructor (props){ super (props) } render(){ return <div>component B</div> } } //容器 class Wrapper extends React.Component { constructor(props){ super (props) } render() { return ( <div> <Link to=‘/a/123‘>组件A</Link> <br/> <Link to=‘/b‘>组件B</Link> {this.props.children} </div> ) } } ReactDOM.render( <Router> <Wrapper> <Route path=‘/a/:id‘ component={A}/> <Route path=‘/b‘ component={B}/> </Wrapper> </Router>, document.getElementById(‘app‘) )
以上是关于react-router传参的主要内容,如果未能解决你的问题,请参考以下文章