You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just wanted to do some conditional rendering of the child based on the heading level, and was surprised there wasn't some magic for me - so I used a State
I wonder if there's a better way, or if not, that it might be documented for future users :)
The current solution is to use the useLevel hook. Something along those lines:
functionParent(){return(<Sectioncomponent={<Hrender={({ level, Component })=>(<ComponentclassName={`font-bold ${headingLevelClasses[level]}${className}`}>{heading}</Component>)}/>}><Child/><divclassName="col-span-2">{children}</div></Section>);}functionChild(){const{ level }=useLevel();// level is 1 level deeper than parent nowif(level!==1){returnnull;}return(<div><HelpLinkclassName="mr-2"helpUrl={helpUrl}/></div>);}
Another solution would be for react-headings to support children as a render prop, but that would make it harder to read then it already is imo. Food for thought.
mmm, so, your comment const { level } = useLevel(); // level is 1 level deeper than parent now is interesting - what i'm experiencing, is that it's a H1 header, but in the child, level == 2 - I'm assuming that your example should just change the if, reasonable, but it does lead to some harder to read code
I just wanted to do some conditional rendering of the child based on the heading level, and was surprised there wasn't some magic for me - so I used a State
I wonder if there's a better way, or if not, that it might be documented for future users :)
The text was updated successfully, but these errors were encountered: