简体   繁体   English

当使用Route render prop而不是Route component prop时,如何访问react-router-dom 4中的url args?

[英]How do you access url args in react-router-dom 4 when using Route render prop instead of Route component prop?

I am using react-router-dom v 4.0.0. 我正在使用react-router-dom v 4.0.0。

In my top-level <App /> component that renders my react router, I have three routes. 在渲染反应路由器的顶级<App />组件中,我有3条路由。 The first two work as expected, the third one does not. 前两个按预期工作,第三个则没有。

render() {
    const pageMain = (
        <PageMain token={this.state.token} />
    );
    const pageActivity = (
        <PageActivity
            projectActivities={this.state.projectActivities}
            projectActivitiesMap={this.state.projectActivitiesMap} />
    );
    return (<Router>
        <div>
            <Route
                path="/"
                component={PageSplash}
            />
            <Route
                path="/app"
                render={props => pageMain}
            />
            <Route
                path="/activity/:activityId/"
                render={props => pageActivity}
            />
        </div>
    </Router>);
}

When I go to www.example.com/ , the router directs me to my PageSplash component as expected. 当我转到www.example.com/时 ,路由器会按预期将我定向到我的PageSplash组件。

When I go to www.example.com/app , the router directs me to my PageMain component as expected. 当我转到www.example.com/app时 ,路由器会按预期将我定向到我的PageMain组件。 Further, in my PageMain component, I have access to this.props.token as expected. 此外,在我的PageMain组件中,我可以按预期访问this.props.token

When I go to www.example.com/activity/12345/ , the router directs me to my PageActivity component as expected. 当我转到www.example.com/activity/12345/时 ,路由器会按预期将我定向到我的PageActivity组件。 In my PageActivity component, I have access to this.props.projectActivities and this.props.projectActivitiesMap as expected. 在我的PageActivity组件中,可以按预期访问this.props.projectActivitiesthis.props.projectActivitiesMap However, I'd like to be able to access the activityId from the url, which should be 12345 in this case. 但是,我希望能够从URL(在这种情况下应为12345)访问activityId。 In PageActivity, I don't receive this.props.params as I thought, from reading countless pages of documentation. 在PageActivity中,我从阅读无数文档页面时并未收到我想的this.props.params

I started by using simple routes like my first one. 我从使用像我的第一个这样的简单路线开始。 Then I needed to pass data from my top-level component to the /app route, so I begin using the <Route /> render prop instead of the component prop. 然后,我需要将数据从顶级组件传递到/ app路由,因此我开始使用<Route /> render道具而不是component道具。 This works fine. 这很好。 Then I needed to pass data and access the url param. 然后,我需要传递数据并访问url参数。 I can access the passed props, but my PageActivity component isn't receiving a params prop. 我可以访问传递的道具,但是我的PageActivity组件没有收到参数道具。 How do I access the activityId in my PageActivity component? 如何访问我的PageActivity组件中的activityId?

I figured out how to do this. 我想出了怎么做。 I needed to inline my PageActivity component, and add {...props} . 我需要内联PageActivity组件,并添加{...props} The problematic route looks like this now: 有问题的路线现在看起来像这样:

<Route path="/activity/:activityId/" render={props => (
    <PageActivity
        projectActivities={this.state.projectActivities}
        projectActivitiesMap={this.state.projectActivitiesMap}
        {...props}
    />
)} />

Also, in react-router-dom 4.0.0, page params are passed in this.props.match.params . 同样,在react-router-dom 4.0.0中,页面参数在this.props.match.params中传递。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:失败的道具类型:提供给“路由”的无效道具“组件”-react-router-dom - Warning: Failed prop type: Invalid prop 'component' supplied to 'Route' - react-router-dom 警告:失败的道具类型:提供给“路由”的“对象”类型的无效道具“组件”,预期的“函数”使用 react-router-dom 错误 - Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function` Error using react-router-dom 在 React Router 中重写自定义路由以使用渲染道具而不是组件道具 - Rewriting custom route to use render prop instead of component prop in React Router 然后如何使用 v6 react-router-dom 在我的路由中渲染一个函数 - How do I then render a function in my route using the v6 react-router-dom 路由更改时 react-router-dom 刷新组件 - react-router-dom refresh component when route changes 在 react 如何使用可重用组件作为 React Component 传递到 react-router-dom 组件道具? - in react how to use reusable component as React Component to pass into react-router-dom Component prop? react-router-dom:如何设置Route以便在两个路径中的任何一个路径上渲染组件? - react-router-dom: How to set up a Route so that it will render a component at either of two paths? React router Route 组件 prop 抛出错误 - React router Route component prop throws an error 如何使用 Typescript 从 react-router-dom 访问路由参数? 例如:`/some-route/:slug` - How to access route params from react-router-dom using Typescript? Ex: `/some-route/:slug` 如何更新正在“react-router-dom”路由上呈现的组件? - How to update component that is being rendered on route of 'react-router-dom'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM