1

I have component contain "React Bootstrap Tab", i want if user select any of these three tabs , to add to current url:-

http://localhost:3000/account

the selected tab key name, so the url change like this:-

http://localhost:3000/account#messages

I have tried by using props.history.push in onSelect function but nothing happened.

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;

3 Answers 3

2

Write below function before render and modify the value part ?tab=${key} inside replace function as per your need.

handleSelect = (key) => {
    this.setState({ key });
    this.props.history.replace({
        hash: `${key}`
    })
}

Inside Tabs, Add function name as shown below

<Tabs onSelect={this.handleSelect} >

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

This makes sense when toggling tab buttons. But reverse seems does not working.
2

Just if anyone need the functionality answered above by @Niraj as a react hook.

import React from "react";
//import bootstrap components
import Tabs from 'react-bootstrap/Tabs';
import Tab from 'react-bootstrap/Tab';

const TabComponent = (props) => {
    //set state from url hash
    const [selectedTab, setSelectedTab] = useState(props.location.hash.substring(1));

    const handleSelect = (e) => {
        //set state
        setSelectedTab(e);
        //update url
        props.history.replace({
            hash: `${e}`
        });
    }

    return (
        //set active key, if no url hash set default to home
        <Tabs 
            activeKey={selectedTab ? selectedTab : 'home'}
            onSelect={(e) => handleSelect(e)}
        >
            <Tab eventKey="home" title="home">
            ....
            </Tab>
            <Tab eventKey="about" title="about">
            ....
            </Tab>
        </Tabs>
    );
}
export default TabComponent;

Comments

0
 const handleTabSelect = (key) => {
    setActiveTabPrimary(key);
    // Append the selected tab key to the URL hash
    window.location.hash = key;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.