Eslint, am I doing something wrong?
So eslint complains about couple of things:
1., docs say something about putting initial, but is it only to avoid eslint warning? I mean I think my code works robustly.
2.
also complains, about reactive context but if I put it like docs suggest
, it no longer works and complains about actual error not just a warning.
3.
This one also seems to work fine but still warns about tracked scope
This one also seems to work fine but still warns about tracked scope
5 Replies
The last is weird, you're setting the signal inside the signal setter? You can do
setToggle(p => !p)
or setToggle(!toggle())
For 1, you don't need to put an initial value but if you don't the initial value will be undefined
2 is fine how you wrote it and someone else might be able to tell you what the eslint warning is trying to say. The reason the changed one might not be working is if your props.onClick doesn't take any arguments and you're passing in e
, it will error
You can omit e and do () => props.onClick()
There's a reasoning behind the warning for the second one but I can't remember it off the top of my head. From what I do remember, it's because event handlers are not reactive? I could be wrong about that partlast one is my bad, its just plain wrong, I tried () => props.onClick() on the second one but I get this ts error
(property) JSX.CustomEventHandlersCamelCase<HTMLButtonElement>.onClick?: JSX.EventHandlerUnion<HTMLButtonElement, MouseEvent> | undefined
Cannot invoke an object which is possibly 'undefined'.ts(2722)
This expression is not callable.
Not all constituents of type 'EventHandlerUnion<HTMLButtonElement, MouseEvent>' are callable.
Type 'BoundEventHandler<HTMLButtonElement, MouseEvent>' has no call signatures.ts(2349)
events on native elements are typed to accept both a function and an array form like
[handler, arg]
where arg
will be passed as the first argument
so typescript wants you to handle the latter case
personally i would ignore the eslint warning which warns assuming that props.onClick
is reactive, when it is convention to treat event handlers as not reactive
for 1., yes props prefixed with initial
are treated as static by the eslint plugin, there is no other effect to naming them differently
3. probably warns because you're reading toggle()
outside a tracked scope (or in a context where the plugin can't deduce whether or not it will be called in a tracked scope)
since you don't intend to track it you can ignore the warningAlright thanks to you both
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View