【发布时间】:2020-10-11 18:23:37
【问题描述】:
我的组件包含“React Bootstrap Tab”,如果用户选择这三个选项卡中的任何一个,我希望添加到当前 url:-
http://localhost:3000/账户
选中的tab键名,所以url变化如下:-
http://localhost:3000/account#messages
我曾尝试在 onSelect 函数中使用 props.history.push,但没有任何反应。
import React from "react";
import KhatmaTable from "../components/Account/KhatmasTable";
import Square from "../components/Account/Square";
import Messages from "../components/Account/Messages";
import { Tabs, Tab } from "react-bootstrap";
class Account extends React.Component {
constructor(props) {
super(props);
this.state = {
user: {},
khatmas: [],
selectedKey: this.props.location.hash
? this.props.location.hash.substring(1)
: "home",
};
}
render() {
const count = [this.state.khatmats];
console.log(this.state.user);
console.log(this.state.user.khatmas ? this.state.user.khatmas.length : "");
return (
<div className="card">
<div className="card-body">
<Tabs
id="controlled-tab-example"
activeKey={this.state.selectedKey}
onSelect={(k) => {
this.setState({ selectedKey: k });
this.props.history.replace = "account" + "#" + k;
}}
>
<Tab eventKey="home" title="home">
<div className="row">
<Square />
</div>
</Tab>
<Tab eventKey="khatmat" title="profile">
<KhatmaTable />
</Tab>
<Tab eventKey="messages" title="messages">
<Messages />
</Tab>
</Tabs>
</div>
</div>
);
}
}
export default Account;
【问题讨论】:
标签: reactjs react-router-dom react-bootstrap