【发布时间】:2020-09-27 16:33:45
【问题描述】:
在我的应用中
<Switch>
<Route path="/" component={DashboardRoute} exact={true} />
<Route path="/patients/:id" component={PatientRoute} />
<Redirect to='/' />
</Switch>
然后在 PatientRoute 我有另一个带有动态路由的 Switch。
<Switch >
{panes.map(pane => {
return <Route path={`/patients/:id/${pane.to}`}>
{pane.render()}
</Route>
})}
</Switch>
我构建了一个名为 RouteTab 的类似 Tab 的组件,它使用 Link 重定向到子患者路线。
<div className="TabButtons">
{panes.map((p, index) => <Link key={p.to} to={p.to} label={p.menuItem}/>)}
</div>
此时一切正常。然而,我的 RouteTab 组件具有响应行为,当 Mobile 使用 Select 来显示项目菜单时。为了模拟 Link 行为,我正在使用 history.push,网址会更改,但页面不会重新呈现。
<Select
value={panes[activeTab].menuItem}
onChange={(e, data) => {
const value = data.value;
const newTabIndex = panes.findIndex(p => p.menuItem === value)
const newTab = panes[newTabIndex]
history.replace(newTab.to) //<--- Here
}}
options={panes.map(p => ({
key: p.menuItem,
value: p.menuItem,
text: p.menuItem,
}))}></Select>
【问题讨论】:
-
将其包裹在
withRouter(HOC) 中。使用this.props.history.push(/route)完成工作。 -
我已经在使用 useHistory 钩子了。这里不一样吗?
-
你能说明你在哪里使用
useHistory吗? -
它在上面的代码框链接中
标签: javascript reactjs react-router browser-history