我知道我們在功能組件中使用 history.push() 并為重定向做出反應路由。但是在類組件中,我們如何在單擊按鈕時重定向
function onSubmit(){
console.log("Cannot use usehistory in this function as this is a class component")
}
//Link is not working in below code
<Link to ="/login">
<Button onClick={this.onSubmit}>Submit</Button>
</Link>
uj5u.com熱心網友回復:
如果你想用歷史來做,你可以使用'react-router'提供的 withRouter 高階組件。你只需要匯入 {withRouter}
import {withRouter} from 'react-router';
并將按鈕所在的組件匯出為:
export default withRouter(Component);
現在您可以通過道具訪問歷史物件。(this.props.history)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/333491.html
標籤:javascript 反应
