【问题标题】:Dynamic React Router with Tabs带有选项卡的动态 React 路由器
【发布时间】:2019-07-16 20:56:31
【问题描述】:

我有一个带有表格的页面,您在其中单击 <tr> 并将用户重定向到另一个页面,如下所示:

// parent component
<tr onClick={() => this.props.history.push(`users/${id}`)}>

在名为 users 的新页面上,有两个选项卡也重定向到其他页面:

// users page receives the id from the parent 
// component with this.props.history.push()
<Tab label='Users' key='users'>
    <div>users page content</div>
</Tab>

// This page must also receive the id from parent component. 
// The structure is similar to users page.
<Tab label='Info' key='info'>
    <Redirect to={'/info/:id'} />
</Tab>

所以,我需要将父页面的 id 传递给 [info page/tab 2] 并使用父页面的 id 随意从 [info page/tab 2] 重定向到 [users/tab 1],例如这个:

[tab 1]: users/12345
[tab 2]: info/12345

我怎样才能达到这个结果? 谢谢! :)

【问题讨论】:

    标签: reactjs react-router react-router-v4 react-router-dom


    【解决方案1】:

    如果你定义你的路线像

    /users/:userId
    /info/:userId
    

    您可以从参数中读取用户 ID 并使用它在页面之间导航

    <Tab label='Info' key='info'>
        <Redirect to={`/info/${this.match.params.userId}`} />
    </Tab>
    

    【讨论】:

    • 我尝试过类似的方法,但 url 看起来像这样 idk 为什么:info/:id12345。然后当我从[info tab] 转到[users tab] 时,网址看起来像这样users/:id:id12345
    • 您可能正在写&lt;Redirect to={'/info/:id'} /&gt;,其中您需要写&lt;Redirect to={`/info/${this.match.params.id}`} /&gt;
    • 现在是这样的:&lt;Redirect to={${routes.INFO}${this.props.match.params.id}} /&gt;,其中 routes.INFO 是 /info/:id
    • 如果我从/info/ 中删除:id,则url 看起来像/info/12345,但我看不到信息页面的内容。
    • export const USERS = '/users/:id' export const INFO = '/info/' export const PARENT = '/parent'
    猜你喜欢
    • 2021-08-08
    • 2020-03-04
    • 2020-09-28
    • 1970-01-01
    • 2015-05-30
    • 2022-06-11
    • 2019-08-28
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多