Replies: 2 comments 1 reply
-
I guess this is happening because I'm running this in a node env. HTML: <html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Testing Preact signals</title>
</head>
<body>
<h1>Testing Preact Signals</h1>
<script type="importmap">
{
"imports": {
"@preact/signals-core": "./node_modules/@preact/signals-core/dist/signals-core.mjs"
}
}
</script>
<script src="./index.js" type="module"></script>
</body>
</html> JS: import { signal, computed, effect, batch } from '@preact/signals-core'
const name = signal('Jane')
const surname = signal('Doe')
const fullName = computed(() => name.value + ' ' + surname.value)
// Logs: "Jane Doe"
const dispose = effect(() => {
console.log(fullName.value)
})
name.value = 'John'
// Destroy effect and subscriptions
dispose()
// Update does nothing, because no one is subscribed anymore.
// Even the computed `fullName` signal won't change, because it knows
// that no one listens to it.
surname.value = 'Doe 3'
console.log(fullName.value) Output: |
Beta Was this translation helpful? Give feedback.
0 replies
-
Your import { signal, computed, effect } from "https://esm.sh/@preact/signals-core";
const s = signal(0);
const c = computed(() => {
console.log('triggering computed');
return s.value
});
const dispose = effect(() => {
console.log('triggering effect');
return c.value;
});
dispose();
s.value = 1;
// Comment and uncomment me
//console.log(c.value); Commented out:
Uncommented:
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I was following the readme guide and got stuck at the following,
Based on the example (and the comment), I expected
dispose()
to unsubsribe from all signals, I expectedconsole.log(fullName.value)
to not have "Doe 2" as the surname; but it does. Is this expected? How do I better understand what "subscription" means in the context of signals?Replicated example: https://replit.com/@painotpi/SingalsTest
Code:
Output:
Beta Was this translation helpful? Give feedback.
All reactions