#solid/reactivity with component ref

20 messages · Page 1 of 1 (latest)

charred patrol
#

Hi. I enabled the solid eslint plugin and going through warnings I came across this case:
https://playground.solidjs.com/anonymous/b30f1656-5be1-4dd2-9d22-11cbf4067c6b

Warning is this:

The reactive variable 'props.ref' should be used within JSX, a tracked scope (like createEffect), or inside an event handler function, or else changes will be ignored.
eslint(solid/reactivity)

Is this a false positive?

Or am I using component ref incorrectly? I couldn't find any documentation except this GH discussion:
https://github.com/solidjs/solid/discussions/564#discussioncomment-4594092

forest jewel
charred patrol
#

I get that, but shouldn't props.ref be special cased or something?

forest jewel
#

the main reason for this warning is

"or else changes will be ignored"

this is mostly used as a heads up for developers to know that you are calling it in
a non tracked scope

forest jewel
charred patrol
#

And do you think there is any problem with using ref like this? Can I just use //eslint-disable-next-line and be done with it?

forest jewel
#

actually I was wrong a bit, but it is unrelated to your issue.
ref on Component is kind of broken.

first: yes, I think you can ignore this warning.
the rule is if your prop does not have "reactivity" you can ignore that warning

forest jewel
#

it does have a minor overhead , but it is more explicit

charred patrol
#

Broken in what sense?

I hit an issue with typescript types, where it's better for the ref to always be a function like this:

const [componentRef, setComponentRef] = createSignal(null)

return <Component ref={setComponentRef} />

otherwise I won't be able to call it in Component to assign it

let componentRef!: ComponentRef;

return <Component ref={componentRef} />

works but Typescript complains, unless I cast.

forest jewel
forest jewel
charred patrol
#

from what I understood in #next thats going to be the only way to do refs in 2.0 anyway

forest jewel
charred patrol
#
GitHub

Solid-specific linting rules for ESLint. Contribute to solidjs-community/eslint-plugin-solid development by creating an account on GitHub.

forest jewel
#

I use setter function most of the times

forest jewel
charred patrol
forest jewel
#

I think ref is actually "reserved" for components too
due to special handling being applied for variable or a function
and the fact that it ignore other values like "string"

but technically any function passed from parent might have reactivity
which you can track. so the rule is correct to warn.

but may be noise as most usage expect a setter without any getter associated to it

so I am now 50%/50% on changing the ref eslint rule

ripe siren
#

Yes, ref is reserved.

when used in native element, it's compiled to call the variable as a functionif it is with untrack otherwise assign it if the expression is assignable just after the element is created

for component, it's also compiled similarly but without the untrack (should be though), and it's up to the component to call props.ref function however it sees fit
simply props.ref({}) top level is also correct

for general users, this might suffice and I might already be leaking too much implementation details rather than user expectation