【发布时间】:2021-04-15 20:36:36
【问题描述】:
点击其他组件时,我正在尝试更改antd中Tab的defaultActiveKey属性。 下面是获取活动键和设置选项卡的代码。
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 };
以下是点击组件并使用上下文调用更改的代码。
let { setActivetab } = useContext(tabActiveContext);
const handleClickThumbnail = () => {
setActivetab("3");
};
我不知道如何在类中绑定上下文来改变状态。 一点帮助将不胜感激。 另外,我想知道当我单击其他组件中的任何内容时更改选项卡的最佳方式。当该类中有来自 App 组件的其他道具时,我不知道如何共享状态。另一方面,我不知道如何在类构造函数中绑定上下文方法。
【问题讨论】:
标签: reactjs antd react-context