I'm trying to change the defaultActiveKey attribute of Tab in antd when click in other component. Below is the code to get active key and set tab.
import { Tabs } from "antd";
import React from "react";
const tabActiveContext = React.createContext({
**setActivetab: (at) => {
}**
});
class CustomTab extends React.Component {
constructor(props) {
super(props);
this.state = {
children:props.children,
otherProps:props.otherProps,
activetabkey: '1',
};
this.setActivetab = this.setActivetab.bind(this);
}
setActivetab() {
this.setState({ activetabkey:'3' });
}
render() {
**const { children, otherProps, activetabkey } = this.state;
console.log(activetabkey)**;
return (
<div className="card-container">
<Tabs
centered
**defaultActiveKey={activetabkey}**
type="card"
size={"small"}
{...otherProps}
tabBarStyle={{ marginBottom: 2 }}
>
{children}
</Tabs>
</div>
);
}
export default CustomTab;
export { tabActiveContext };
Below is the code to click on component and call to change using context.
let { setActivetab } = useContext(tabActiveContext);
const handleClickThumbnail = () => {
setActivetab("3");
};
I dont know how to bind context in class to change the state. Little help would be appreciated. In addition, I want to know the best way to change tab when I click on anything in other component. I've no idea about sharing state when there are some other props in that class from App component other than state. On the other, hand I've no idea how to bind context method in class constructor.