Open 9Morello opened 1 year ago
when updated
Are you referring to HMR? Might be an issue for Prefresh.
Edit: Hmm, looks like it should be supported, per https://github.com/preactjs/preset-vite/pull/58
@rschristian Hello! Yes, I am referring to HMR. Do you think I should recreate this issue on the Prefresh repo?
Edit: I've just tested this with React instead of Preact and the same issue shows up. Changing other parts of the JSX (such as the text inside the p
tag) causes count
to go back to its default value, and if you use the useState
hook instead, the value is kept after a hot reload as expected.
Do you think I should recreate this issue on the Prefresh repo?
Can just CC @JoviDeCroock here, he would know best I believe. I don't know where we broke support, assuming it was working once.
I've just tested this with React instead of Preact and the same issue shows up.
React is a different beast entirely. Given the relationship of an external org trying to patch React to support signals and its ergonomics, I'm not sure HMR support in React is something that can be hoped for.
@9Morello I've ran into this issue too but I was thinking that was the norm if the computed values weren't wrapped in a useComputed hook. If I use a useSignal, I wrap the return value using that signal's value in a useComputed or I create a signal outside of the component function and use a return statement in the useEffect of the component to clean up (reset) the signal's value on un-mounting. I've done this for a couple of components already and I don't think I've ran into any optimization bugs.
I have tested this using Vite's
preact-ts
template. Using theuseState
hook keeps the state after the component gets reloaded because of a change:With the above, I can change the text inside the
p
tag, or change the counter display tocount * 2
, andcount
's value stays the same as the component reloads.However, using the same component with the
useSignal
hook, it doesn't work:Any change to text or to the function inside the
onClick
parameter resetscount
to 0. Is there any way to avoid this? Am I misunderstanding something? Sorry for the trouble.