-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: What's a tracked scope? #8
Comments
Hi, thanks for the question, and I don't mind you asking here at all. Tracked scopes are mentioned a few times in the Solid docs; they're basically any function or expression where it's okay to have reactive variables because Solid is watching for changes. The idiomatic example is function Component() {
const [signal, setSignal] = createSignal(1);
setTimeout(() => {
setSignal(2);
}, 1000);
createEffect(() => console.log(signal())); // tracked scope, logs 1, then 2 when signal changes
return <div>{signal()}</div>; // tracked scope, displays 1, then 2 when signal changes
} The function Component() {
const [signal, setSignal] = createSignal(1);
setTimeout(() => {
setSignal(2);
});
console.log(signal()); // only logs 1, never logs 2
} I would have liked to link to an answer to you question in the error message, but couldn't find a good explanation. Now, the const [signal] = createSignal(1);
function createEffectCopy(...args) {
return createEffect(...args);
}
createEffectCopy(() => console.log(signal())); // warns, even though this is fine There's not a perfectly accurate solution here: the Thanks for reading, hope that's helpful, and in a future release the rule won't warn when your hook is named |
Thank you for the extremely detailed answer, this definitely helped a lot! |
No problem! For now, just disable the rule on each line with |
Here's another example I'm having trouble with Specifically line 83. I'm also seeing the same warning with other <For> components. Maybe it's late and I don't quite understand the rule, or maybe it's another edge case. I'd like to find out :-) |
Hi @millette, I double checked and I believe this case is actually reporting a bug. The offending code: onClick={setSelected.bind(
null,
i() /* eslint-disable-line solid/reactivity */
)} From the docs:
Unlike React, where you render multiple times and In your code, onClick={() => setSelected(i())} With Solid, there is no performance penalty to this code. The Let me know if there's anything else I can help you with! |
@joshwilsonvu Thanks for looking into it. I checked the docs and also tried <button onClick={[setSelected, i()]}> That's still triggering the same warning. When I use your suggestion, everything works and no warning. I believe the array version should also work without a warning. |
One last example, this time with |
I checked the array syntax and it doesn't seem to work in practice. In my example it always logs the initial value and never updates, which fits with "the bindings are not reactive" from the docs. I think the warning is correct: you have to use a function if you're using signals or props. The array syntax seems to be for non-reactive arguments, like constant strings/numbers/etc. With your |
@imedadel The issue with custom hooks should be resolved in |
Hello Josh! I'm enjoying this ESLint plugin, however, I'm having trouble understanding the meaning of a tracked scope and the examples aren't quite answering my question. In my case, I wanted to pass a prop to a custom hook/function but I'm getting either
The reactive variable 'localRef' should be used within a tracked scope
orThis function should be passed to a tracked scope because it contains reactivity.
Example:
What am I missing? 😩
And, sorry if this is not the right place to ask!
The text was updated successfully, but these errors were encountered: