Am I allowed to use useState and useEffect when using signals? #651
-
Hello people, import { render } from 'preact';
import { signal } from '@preact/signals';
import { useEffect, useState } from 'preact/hooks';
const counter = signal(0);
export function App() {
const [stateData, setStateData] = useState(0);
useEffect(() => {
setInterval(()=> {
counter.value = counter.value + 1;
console.log(`increasing counter ${counter.value}`)
}, 5000)
}, [])
useEffect(() => {
setInterval(() => {
console.log(`increasing stateData`)
setStateData(prev => prev+5)
}, 1000);
}, []);
return (
<div>
<h1>{`Output ${counter.value + stateData}` }</h1>
</div>
);
}
render(<App />, document.getElementById('app')); package.json {
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"@preact/signals": "^2.0.1",
"preact": "^10.20.1"
},
"devDependencies": {
"@preact/preset-vite": "^2.9.3",
"typescript": "^5.7.3",
"vite": "^6.0.4"
}
}
I see no re-render every second (when useState fires) but it is only every 5 seconds (when the signal fires) that I can see the 'real' value of the useState variable. It is enough to use Am I missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
did you forget the return function? useEffect(() => {
const interval = setInterval(() => {
console.log('This will run every second!');
}, 1000);
return () => clearInterval(interval);
}, []); |
Beta Was this translation helpful? Give feedback.
-
This works perfectly for me https://stackblitz.com/edit/vitejs-vite-1auxunjd?file=src%2Fapp.tsx not sure what you're seeing but this might be related to #630. Do you have any specific bundler that isn't vite or is your preact version older than 10.12 in your real application? There might also be something going on where you forget to cleanup the set interval. Being completely clear, this is expected to work but there are some nuances with state settling as lined out in that issue. Before that fix there was an issue where conflicting update could cause infinite loops, with the fix lined out in #630 mixing signals and state should work in everything as of v10.12. That being said, I've heard a bug in #650 that mentions something similar to your issue. However it's been hard to dig to the bottom of that issue so if you can give any more pointers to your specific case or provide a way to reproduce it I'd love to take a look. |
Beta Was this translation helpful? Give feedback.
This works perfectly for me https://stackblitz.com/edit/vitejs-vite-1auxunjd?file=src%2Fapp.tsx not sure what you're seeing but this might be related to #630. Do you have any specific bundler that isn't vite or is your preact version older than 10.12 in your real application?
There might also be something going on where you forget to cleanup the set interval.
Being completely clear, this is expected to work but there are some nuances with state settling as lined out in that issue. Before that fix there was an issue where conflicting update could cause infinite loops, with the fix lined out in #630 mixing signals and state should work in everything as of v10.12.
That being said, I've heard…