-- [EDIT - 6th October 2021] ----------------------------
Since lit 2.0.0 has been released my answer below
is completely obsolete and unnecessary!
Please check https://lit.dev/docs/api/directives/#ref
for the right solution.
---------------------------------------------------------
Even if this is not exactly what I have asked for:
Depending on the concrete use case, one option to consider is the use of directives.
In my very special use-case it was for example (with a little luck and a some tricks) possible to simulate more or less that ref object behavior.
const inputRef = useElementRef() // { current: null, bind: (special-lit-html-directive) }
...
return html`<div><input ref=${inputRef.bind}/></div>`
In my use case I could do the following:
- Before rendering, set
elementRef.current
to null
- Make sure that
elementRef.current
cannot be read while the component is rerendered (elementRef.current
is not needed while rendering and an exception will be thrown if someone tries to read it in render phase)
- That
elementRef.bind
directive will fill elementRef.current
with the actual DOM element if available.
- After that,
elementRef.current
can be read again.
querySelector
to get a reference to the rendered input, are you using lit-html only or are you using it in LitElement? I can post a more detailed answer with an example matching what you need – Ochlocracyref
directive has been added to Lit lit.dev/docs/api/directives/#ref – Perfectible