#How can I execute a function from parent?

14 messages · Page 1 of 1 (latest)

shadow herald
#

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>
}

Is something like this possible?

autumn trail
#

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)

shadow herald
#

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?
}
autumn trail
#

See solids types for reference

shadow herald
#

I saw Ref but that didnt seem to work properly

autumn trail
#

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

shadow herald
#

ah

autumn trail
#

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

shadow herald
#

i see