第一次正确执行该功能,之后一直单击按钮 - 不。我认为它会在某个地方终止。我认为在某些检查点中,出于某种原因,该值是空的,也许这就是它不执行的原因,ebacsie 第一次总是正确执行,之后总是 - 不。之后的所有时间我都看不到“功能几乎完成执行”的控制台日志
预期的行为是有一个图标箭头,onClick 将在导航中打开/关闭主链接的子链接列表。
像这样:
图标是“>”,链接是 somelink
当 somelink 被点击时,我想要的导航外观是:“<” somelink link1 link2 link3 (以便打开子菜单)
我正在使用 React 钩子。请帮帮我,我做错了什么?
const ServiceConditions = () => {
const [arrowIsClicked, setArrowIsClicked] = React.useState(false);
const handleSublinksExpandCollapse = (e : React.MouseEvent<SVGSVGElement>) => {
console.log("arrow is clicked");
if (arrowIsClicked) return;
setArrowIsClicked(true);
if ((e.target as HTMLElement).style.transform === "rotate(0deg)") {
(e.target as HTMLElement).style.transform = "rotate(180deg)";
if (!e.target) console.log(e.target); return
const parent = (e.target as HTMLElement).parentNode;
if (!parent) console.log(e.target); return
const nextSibling = parent.nextSibling;
if (!nextSibling) console.log(e.target); return
(nextSibling as HTMLElement).style.display = "block";
console.log("function has almost done executing")
setArrowIsClicked(false)
} else { setArrowIsClicked(true); (e.target as HTMLElement).style.transform = "rotate(0deg)";
if (!e.target) console.log(e.target); return
const parent = (e.target as HTMLElement).parentNode;
if (!parent) console.log(e.target); return
const nextSibling = parent.nextSibling;
if (!nextSibling) console.log(e.target); return
(nextSibling as HTMLElement).style.display = "none"; setArrowIsClicked(false); }
}
return (
<Container>
{ isDesktop &&
<><NavigationContainer>
<StyledScrollSpy
scrollTargetIds={["section_1", "section_2", "section_3"]}
offset={100}
activeNavClass="is-active"
scrollDuration="1000"
headerBackground="true"
>
<List>
<MainRef><Line1><MainRefTypography variant="body1"><a href="#">Home</a></MainRefTypography><IconStyled id="ad-ap" onClick={(e : React.MouseEvent<SVGSVGElement>) => {handleSublinksExpandCollapse(e)}}></IconStyled></Line1><div><LinksInsideList>
<MainRef><LinkInsideTypography variant="body4"><a href="#section_1">Section 1</a></LinkInsideTypography></MainRef>
<MainRef><LinkInsideTypography variant="body4"><a href="#section_2">Section 2</a></LinkInsideTypography></MainRef>
<MainRef><LinkInsideTypography variant="body4"><a href="#section_3">Section 3</a></LinkInsideTypography></MainRef>
</LinksInsideList></div></MainRef>
</List>
</StyledScrollSpy>
</NavigationContainer></> }
<Articles>
<PageName variant={isDesktop ? "h3" : "h1"}>SERVICE CONDITIONS</PageName>
<Bar align={BarAlign.Left} />
<Spacing mobile={3} desktop={3}/>
<div style={{"height": "400px", width: "100%"}}><span>Welcome!</span></div>
<div id="section_1" style={{"height": "500px", width: "100%"}}><span>Section 1</span></div>
<div id="section_2" style={{"height": "500px", width: "100%"}}><span>Section 2</span></div>
<div id="section_3" style={{"height": "500px", width: "100%"}}><span>Section 3</span></div>
</Articles>
</Container>
);
};
export default ServiceConditions;