【发布时间】:2022-01-01 07:09:41
【问题描述】:
当用户单击基于类的组件(名为 Counter)中的指定按钮(在本例中名为 Navigate)时,我试图重定向到另一个路由器。我为此使用<Navigate />。它不会重定向到我指定重定向到的页面(主页的路径='/')。此外,我没有收到任何错误。有人,请告诉我使用<Navigate /> 的最佳方式。
供参考,代码为:
import React, {Component} from "react";
import {Navigate} from 'react-router-dom'
class Counter extends Component {
constructor(props){
super(props)
this.state = {
}
this.navigate = this.navigate.bind(this)
}
navigate(e){
return <Navigate to="/" />
}
render(){
return (
<div className='text-center'>
<h1>Hello there, this is a counter app</h1>
<button onClick={this.navigate} >Navigate</button>
</div>
)
}
}
export default Counter
【问题讨论】:
标签: reactjs redirect navigation