Access ref in child component when using forwarding refs
I would like to use a forwarding ref as described in https://docs.solidjs.com/concepts/refs#forwarding-refs but also access the ref in the child component itself, like:
Is it possible?
4 Replies
<canvas ref={(el) => props.ref = anotherRef = el} />
the ref
is passed as a callback function.
It works, thanks!
I'm a bit lost how refs work š¦
What I try to achieve is to create a component which wraps various children 3-4 components deep, and to propagate the ref of the deepest component to the top level component.
It is something like:
(I know it is not ideal from an "encapsulation" viewpoint. I have other ideas how to solve the problem but I'm very curious whether it is possible to propagate
ref
s "upwards".)
What I would like is to know the ref of the <input>
at the level of ValidatingInputFieldWithLabel
, so I can add event listeners to the <input>
.
What is the best solution?
Besides, can someone explain what is the magic behind the ref feature? Until now I thought I understand it but now I'm a bit confused, e.g. how can I know that it is a simple Element or a Function? Or should I always check it at runtime in my custom components?
Thanks.so I can add event listenersWhy not just pass those listeners down through
InputFieldWithLabel
and InputField
as props and have InputField
set the onInput
etc. props?
how can I know that it is a simple Element or a Function?I'd say it is the other around. The
ref
prop is part of the JSX transform. If a function is passed it is called . Otherwise it probably creates the necessary function needed to assign the variable identified at runtime.
i.e. the JSX transform makes sure that <canvas ref={canvasRef} />
is actually <canvas ref={(el) => canvasRef = el} />
;
So the props.ref
has to be a function.Thanks @peerreynders for the detailed explanation.
I will pass listeners instead, it is a much more clean solution š