从父路由访问子路由参数。反应路由器

时间:2017-06-08 21:36:53

标签: react-router-v4

考虑更改后路由的下拉菜单。

逻辑上我们有一个布局:

<Things>
    {/* Dropdown menu that, when item is selected, 
        routes to appropriate Thing-specific path */}
    <ThingsDropdown />

    {/* Component that represents the currently selected thing. */}
    <Thing />
</Things>

这个想法是这里的顶级是/ things /,并从下拉路线中选择一个项目到/ things /:thingId /.

当直接加载/ things /:thingId /时,下拉列表的值应默认为:thingId。但是父组件(Things)无法访问特定于Thing的参数。

这不是React Router独有的问题。 Ember的路由系统具有相同的限制。

处理此用例的推荐模式是什么?必须指定当前的thingId两次(一次作为参数,一次处于状态以便可以在父组件中正确设置)似乎很浪费。

感谢。

亚当

1 个答案:

答案 0 :(得分:0)

经过一些研究后,我确定这里的解决方案是使用始终呈现的Route,并通过查看传递给组件的match参数(如果已设置)来确定所选的thingId。

相关问题