【发布时间】:2021-10-11 16:58:44
【问题描述】:
我有一组抽屉项目,我正在尝试切换其状态。我写了一个函数,我认为它可以用来切换各个抽屉项目,但它不起作用。我收到了TypeError: drawer.setState is not a function。不完全确定如何让它工作。有没有人有关于如何做到这一点的任何提示?提前致谢!
class CategoryList extends React.Component<CategoryListProps, CategoryListState> {
constructor(props) {
super(props);
const drawers = this.props.items.map((item) => ({
open: false,
heading: item.heading,
drawerHeading: item.drawerHeading,
drawerContent: item.drawerContent,
}));
this.state = { drawers };
this.handleToggle = this.handleToggle.bind(this);
}
handleToggle = (drawer) => {
drawer.setState({ open: !drawer.open });
};
render() {
return (
<div className="pb-10">
<h5 className="pb-8 text-white md:text-center">{this.props.heading}</h5>
<div className="">
{this.state.drawers.map((drawer) => (
<div className="flex flex-col items-baseline w-full py-6 text-white border-b md:flex-row border-b-white">
<h3 className="flex-shrink-0">{drawer.heading}</h3>
<div className="right">
<p className="font-serif text-xl">{drawer.drawerHeading}</p>
{drawer.open && <CategoryListDrawer drawer={drawer} />}
</div>
<div className={`flex items-center self-end justify-center w-12 h-12 ml-auto transform border border-white rounded-full toggle-btn cursor-pointer ${drawer.open === true ? 'open' : ''}`} onClick={this.handleToggle(drawer)}>
<span className="w-6 h-6 overflow-hidden transition-transform duration-75 transform toggle"></span>
</div>
</div>
))}
</div>
</div>
);
}
}
【问题讨论】:
标签: javascript reactjs react-state-management