【发布时间】:2019-10-13 13:48:14
【问题描述】:
我正在对按钮单击事件进行路由
redirect(){
return <Redirect to='/chat' />;
}
它不工作。
我的主页-基础组件:
import React, { Fragment } from 'react';
render() {
const { value } = this.state;
return (
<Router>
<div>
<Switch>
<Route exact={true} path="/" component={Home} />
<Route path="/notify" component={Notify} />
<Route path="/chat" component={Chat}/>
</Switch>
</div>
<footer className="foot">
<BottomNavigation className="navbar navbar-default navbar-static-bottom navbar-fixed-bottom" value={value} onChange={this.handleChange} className={this.props.root}>
<div className="navi">
<Link to="/"> <BottomNavigationAction label="Home" value="Home" icon={<HomeIcon />} /></Link>
</div>
<div className="navi">
<Link to="/notify"><BottomNavigationAction label="Notification" value="Notification" icon={<NotifyIcon />} /></Link>
</div>
<div className="navi">
<Link to="/chat"> <BottomNavigationAction label="Listen" value="Listen" icon={<LocationOnIcon />} /></Link>
</div>
</BottomNavigation>
</footer>
我必须从我的子组件重定向到另一个组件, 这是我的子组件:
<IconButton className={this.props.iconButton} aria-label="Search" onClick={this.redirect}>
<SearchIcon />
</IconButton>
redirect(){
return <Redirect to='/chat' />;
}
所以,但我也没有收到任何控制台错误。
【问题讨论】:
-
它是因为
Redirect是一个 ui 元素,你必须渲染它,而 onClick 是一个事件处理程序,它不会渲染任何东西,它只会执行处理程序函数的主体。要解决您的问题,请使用this.props.history.push('/chat')
标签: reactjs typescript react-router