【问题标题】:How to pass extra params with react router如何使用反应路由器传递额外的参数
【发布时间】:2016-03-20 04:53:39
【问题描述】:

我定义了以下路由,其中​​在 slug 上呈现一个新组件,并且孔 api 请求由通量管理。因为 API 端点需要一个 id,但同时我想将 slug 保留在我的 url 中,我想以某种方式从我的 Workshops 组件传递 ID:

export default class Root extends Component {
    static propTypes = {
        history: PropTypes.object.isRequired
    }

    render() {
        const { history } = this.props;
        return (
            <Router history={history}>
                <Route component={App}>
                    <Route component={Layout}>
                        <Route path='/' component={Workshops} />
                        <Route path='/:slug' component={BookVoucher} />
                    </Route>
                </Route>
            </Router>
        );
    }

}

我尝试从 Workshops 组件向 BookVoucher 传递一个额外的 ID,但我不知道该怎么做

<Link to={ workshop.slug } id ={ workshop.ID }  > {workshop.title.rendered }</Link><

【问题讨论】:

标签: reactjs parameter-passing components react-router


【解决方案1】:

要传递更多信息,您需要使用查询参数。请参阅文档中的this example

相关位:

render() {
  let { slug } = this.props.params
  let { query } = this.props.location
  let age = query && query.extraId ? '_default' : ''
  // ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-18
    • 2014-10-29
    • 1970-01-01
    • 2019-11-15
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多