Skip to content

Instantly share code, notes, and snippets.

@dmail
Last active February 17, 2025 14:11
Show Gist options
  • Save dmail/05d417646954db30356ff2d492d3f9d5 to your computer and use it in GitHub Desktop.
Save dmail/05d417646954db30356ff2d492d3f9d5 to your computer and use it in GitHub Desktop.
Pass props to child from parent in preact
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