S
SolidJS•2y ago
CatNoir

How can I execute a function from parent?

How does ref work? I want to execute a function from parent, something like this:
// Parent
function Parent () {
let something;

return (
<Child something={something}/>
)
}

// Child
function Child(props) {
props.something = () => console.log("hello");

return <div>Hello</div>
}
// Parent
function Parent () {
let something;

return (
<Child something={something}/>
)
}

// Child
function Child(props) {
props.something = () => console.log("hello");

return <div>Hello</div>
}
Is something like this possible?
8 Replies
thetarnav
thetarnav•2y ago
ref is a special prop that compiles to ref={e => your_variable = e}. Other props don't get that treatment. Just use the function form So to pass a ref from child (or any other prop) you do props.ref(your_value)
CatNoir
CatNoirOP•2y ago
I meant to say that i wanna execute a function from parent, I wanna make my own ref kinda thing ah 🤔 That seems to work, thanks 🙂 do you know how i could use it with typescript?
interface Props {
ref?: what goes here?
}
interface Props {
ref?: what goes here?
}
thetarnav
thetarnav•2y ago
See solids types for reference
CatNoir
CatNoirOP•2y ago
I saw Ref but that didnt seem to work properly
thetarnav
thetarnav•2y ago
it's T | (v: T) => void or something but you have to lie to typescript when using it, as it always compiles to a function so it cannot be T
CatNoir
CatNoirOP•2y ago
ah
thetarnav
thetarnav•2y ago
there is mergeRefs in solid-primitives that aims to help with refs a little bit. But it's not necessary. I'll just hide the type assertion as implementation detail
CatNoir
CatNoirOP•2y ago
i see ill just use as any for now 😅
Want results from more Discord servers?
Add your server