import { forwardRef } from 'preact-forwardref';
const Foo = forwardRef((props, ref) => {
return <div ref={ref} />
});
render(<Foo ref={div => console.log(div)} />, document.body);
Last active
July 25, 2023 12:54
-
-
Save developit/974b5e4e582df8e954c5a7981603cd37 to your computer and use it in GitHub Desktop.
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
{ | |
"name": "preact-forwardref", | |
"version": "0.1.0", | |
"description": "standalone preact forwardRef", | |
"repo": "gist:974b5e4e582", | |
"homepage": "https://gist.github.com/974b5e4e582df8e954c5a7981603cd37", | |
"type": "module", | |
"main": "preact-forwardref.js", | |
"types": "./types.d.ts", | |
"scripts": { | |
"prepack": "mv '*Preact forwardRef.md' README.md", | |
"postpack": "mv README.md '*Preact forwardRef.md'" | |
} | |
} |
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{options}from'preact'; | |
options.__b = function(o,v) { | |
if (v.type && v.type._f && (v.props.ref=v.ref)) v.ref = null; | |
o && o(v); | |
}.bind(0,options.__b); | |
export function forwardRef(fn) { | |
function F(p) {var ref=p.ref;delete p.ref;return fn(p, ref)} | |
return F._f = F; | |
} |
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 { Ref, FunctionComponent, ComponentChild } from "preact"; | |
export function forwardRef<RefType, Props>( | |
component: (props: Props, ref: Ref<RefType>) => ComponentChild | |
): FunctionComponent<Props & { ref?: Ref<RefType> }>; |
Thanks @rubencodes!! I've just added your types to the package and published 0.2.0
.
Amazing, thank you! 🙇
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case it's helpful to anyone, I wrote up some (maybe correct? it seems to work right) types: