【发布时间】:2019-05-24 21:49:51
【问题描述】:
我正在尝试实现一个可折叠组件。我已经设计了它,例如,单击一个按钮,就会出现一个动态文本块。我制作了一个功能组件并在类中使用标签。组件的名称是 CustomAccordion.jsx 并在 Container.jsx 中使用该组件
我尝试为 onClick 事件创建一个按钮和一个函数。
CustonAccordion.jsx 的一部分
const handleToggle = () : string =>{
let content = this.nextElementSibling;
if (content.style.maxHeight){
content.style.maxHeight = null;
}else{
content.style.maxHeight = content.scrollHeight +'px';
}
}
export default function CustomAccordion(props: PropType): React.Component<*> {
const { title, children } = props
return(
<div>
<AccordionButton onClick={() => this.handleToggle()}>{title}</AccordionButton>
<AccordionContent>
<p>{children}
</p>
</AccordionContent>
</div>
)
}
调用Container.jsx的一部分
<CustomAccordion title = {this.props.name}>
<p>This is the text passed to component.</p>
</CustomAccordion>
<br />
这并没有显示展开的文本,并且似乎单击事件无法正常工作。我是反应新手,猜测语法可能不正确。
【问题讨论】:
标签: reactjs button collapsable