React JS,无法重定向组件



我正在使用哈希路由器,我的重定向功能无法按预期工作。

用户删除组件后,以下函数应重定向到我的主页

const handledelete=()=>{
apiService('/d/, "DELETE").then(
()=> { <Redirect to='/' />}
)
};

所需路线

<Route exact path="/" component={SimpleCard}/>

父元素

<HashRouter>
<Base/>
</HashRouter>

我尝试了与路由器一起使用的历史.push方法,但没有工作

尝试这个希望这个帮助.

state = {
redirect : false;
}
const handledelete=()=>{
apiService('/d/, "DELETE").then(
()=> {
this.setState({redirect:true})
}
)
};

render(){
if(this.state.redirect)
<Redirect to="/path"/>
return(
<></>
)
}

最新更新