React-如何从React路由器获取最后一个位置

问题描述 投票:0回答:1

我有一个按钮,应根据先前的位置进行重定向。如果该条目存在并且来自同一域,则应将其重定向到最后一个条目,如果不存在,则应将其重定向到默认页面。现在,我只是使用按钮上的history.goBack(),这意味着如果我从其他网站访问该页面,它将重定向到该网站。我想避免这种情况,并将其重定向到默认页面。我还需要检查最后一个条目是否来自我应用程序中的特定页面,因为那样的话我也希望避免重定向到它。

          <IconButton aria-label="Clear" onClick={() => history.goBack()}>
            <ClearIcon/>
          </IconButton>

为了管理,我需要访问历史记录对象中的条目,但在react router docs中看不到。如何使用react-router管理这种情况?

reactjs react-router browser-history
1个回答
0
投票

[historylocationmatch是由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)
© www.soinside.com 2019 - 2024. All rights reserved.