引入注册withRouter
import { withRouter } from 'react-router-dom';
export default withRouter(DetailComponent)
在跳转页面按钮添加功能
skipToSource = () =>{
this.props.history.push({
pathname:'/source', //跳转页面name
state:{ //参数
id:2
}
})
}
在目标页面接受参数
componentDidMount() {
if (this.props.location.state === undefined) {
//如果不是从跳转页面过来的activeKey设为1, tab显示第一个
this.setState({
activeKey: '1'
});
}else{
//否则设置为想要显示的tab
let state = this.props.location.state;
this.setState({
activeKey: state.id+''
});
}
}
在antd的tabs中,应该设置activeKey 而不是defaultActiveKey,使用onChange事件
<Tabs activeKey={activeKey} onChange={this.handleChangeActiveTab} >
<TabPane tab="来源类型" key="1">
</TabPane>
<TabPane tab="来源网站" key="2">
</TabPane>
</Tabs>
//选择tab
handleChangeActiveTab = (key) =>{
this.setState({
activeKey: key
});
}