
为了
history在
App组件中使用,请与配合使用
withRouter。
withRouter仅当您的组件未收到时,才需要使用
Routerprops,
如果您的组件是 由路由器渲染的 组件的 嵌套子代, 或者 您尚未将路由器道具传递给它, 或者 该组件根本未链接到路由器
,并且作为独立于 该组件 的组件呈现,则 可能会发生这种情况路线。
import React from 'react';import { Route , withRouter} from 'react-router-dom';import Dashboard from './Dashboard';import Bldgs from './Bldgs';var selectedTab;class App extends React.Component { constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); selectedTab = 0; } handleClick(value) { selectedTab = value; // console.log(selectedTab); this.props.history.push('/Bldgs'); // console.log(this.props); } render() { var _this = this; return ( <div> <Route exact path="/" render={(props) => <Dashboard {...props} handleClick={_this.handleClick} />} /> <Route path="/Bldgs" component={Bldgs} curTab={selectedTab} /> </div> ); }}export default withRouter(App);文档
上withRouter
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)