React-router-dom 和 Redirect 没有被添加到历史记录中? [英] React-router-dom and Redirect not being added to history?
问题描述
大纲:
我目前正在尝试学习 react/redux/router
.作为一个实践项目,我正在开发一个基本的联系人/客户管理应用程序.在使用 react-router-dom
的 Redirect
组件时.我无法让后退/前进导航按预期工作.
详情:
我有一个表,其中包含位于 点击 这是位于父 </BrowserRouter>);} 问题: 重定向后,如果您单击 我曾尝试将 警告:( 我知道我应该使用 找到了答案,我需要将 来自文档: 推:布尔 如果为 true,重定向会将新条目推送到历史记录中,而不是替换当前条目. Outline: I'm currently trying to learn Details: I have a table with a list of entries located at The click on This is the router which is located in the parent Question: After the redirect, if you click the back button from I have tried wrapping the Caveat: ( I know that I should be using Found the answer, I needed to add From the docs: push: bool When true, redirecting will push a new entry onto the history instead of replacing the current one.
这篇关于React-router-dom 和 Redirect 没有被添加到历史记录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!/contacts/
的条目列表,当您单击表行之一时,我想使用值集 Redirect
通过 的 onClick 属性. this.setState({ id: d._id })}> 改变 state.id
到 clientID
的唯一值.这会导致表渲染方法中的 Redirect
为真,从而触发 Redirect
.render() {//'/contacts/' {Contacts} 组件的渲染方法返回(...//表头{ this.state.id &&<重定向到={"/contacts/card/"+this.state.id}/>}...//表格内容)}
App.js
render() {返回 (<BrowserRouter basename="/" ><div><导航栏/><主要><开关><路由精确路径="/" component={Login}/><路线精确路径=/联系人"组件={联系人}/><路线精确的path="/contacts/card/:clientID" component={ContactCard}/></开关></main>
/contacts/card/:clientID
中的后退按钮,浏览器不会返回到 /contacts/
.相反,它会循环浏览我查看过的任何以前的 :clientID
URL. 包装在路由器 标签中,但它破坏了样式.
this.state.id
)Redux
来保存重定向函数的 this.state.id
的值.我还没有完全掌握如何使用单个 redux reducer 管理多个值.一旦我这样做,我会用适当的方法更新问题.push
添加到我的
组件中,例如所以.<重定向推送到={`/contacts/card/${this.state.id}`}/>
<重定向/>
react/redux/router
. As a practice project, I'm working on a basic contact/client management app. While using react-router-dom
s Redirect
component. I can't get the back/forward navigation to work as expected./contacts/
and when you click on one of the table rows, I want to Redirect
using a value set by the <tr>
s onClick attribute.<tr key={d._id} onClick={()=>this.setState({ id: d._id })}>
changes state.id
to the clientID
s unique value. This results in a Redirect
being true in the tables render method, triggering the Redirect
.render() { // render method of '/contacts/' {Contacts} component
return(
... // table head
{ this.state.id && <Redirect to={"/contacts/card/"+this.state.id}/> }
...// table content
)
}
App.js
render() {
return (
<BrowserRouter basename="/" >
<div>
<NavBar/>
<main>
<Switch>
<Route exact path="/" component={Login}/>
<Route
exact path="/contacts" component={Contacts}/>
<Route
exact
path="/contacts/card/:clientID" component={ContactCard}/>
</Switch>
</main>
</div>
</BrowserRouter>
);
}
/contacts/card/:clientID
, the browser doesn't go back to /contacts/
. instead, it cycles through any previous :clientID
URLs that I've looked at.<tr>
in a router <Link to={}>
tag, but it destroyed styles.this.state.id
)Redux
to hold the value of this.state.id
for my redirect function. I haven't fully grasped how to mange multiple values with a single redux reducer yet. I will update question with the appropriate method once I do.push
to my <Redirect/>
component like so.<Redirect push to={`/contacts/card/${this.state.id}`}/>
<Redirect/>
登录
关闭