Last active
February 17, 2025 14:11
-
-
Save dmail/05d417646954db30356ff2d492d3f9d5 to your computer and use it in GitHub Desktop.
Pass props to child from parent in preact
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { cloneElement, toChildArray } from "preact"; | |
const AutoTargetBlank = ({ children }) => { | |
children = toChildArray(children); | |
return ( | |
<div> | |
{children.map((child) => { | |
return cloneElement(child, { target: '_blank' }); | |
})} | |
</div> | |
) | |
// we could also just render children if no div is needed | |
// return <>{chidren.map(child => cloneElement(child, { target: '_blank' })}</>; | |
} | |
// Example | |
<AutoTargetBlank> | |
<a href="http://example.com" | |
</AutoTargetBlank> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment