我有一个按钮,应根据先前的位置进行重定向。如果该条目存在并且来自同一域,则应将其重定向到最后一个条目,如果不存在,则应将其重定向到默认页面。现在,我只是使用按钮上的history.goBack()
,这意味着如果我从其他网站访问该页面,它将重定向到该网站。我想避免这种情况,并将其重定向到默认页面。我还需要检查最后一个条目是否来自我应用程序中的特定页面,因为那样的话我也希望避免重定向到它。
<IconButton aria-label="Clear" onClick={() => history.goBack()}>
<ClearIcon/>
</IconButton>
为了管理,我需要访问历史记录对象中的条目,但在react router docs中看不到。如何使用react-router管理这种情况?
[history
,location
和match
是由withRouter
HOC注入的特殊道具。
import { withRouter } from 'react-router-dom'
const Component = withRouter({location, history, match}) => {/*...*/}
class Component extends React.Component{
render(){
const {location, history, match} = this.props
return /*...*/
}
}
export default withRouter(Component)