Closed Wombat42 closed 3 years ago
@Wombat42 Thanks for your reporting, we discussed this in discord I think( in jotai part). The reason why everything lost is that we use object reference and hot-reload can't keep the same object reference after every change.
We had some discussions in zustand too. Valtio as well as zustand is module state which lays outside react, and it's limited in terms of fast-refresh support. I'm not sure if there's any workaround other than splitting state files.
Isn't Vue model a bit similar?
I'm using Valtio with Vite bundler and not even HMR works
Never used vite bundler. Would you be able to create a small repro that we can try on our end?
FYI Vite works well with Valtio now
Let me close this issue. Basically, hot reload works as expected if state is defined in a different file. Suggestions are welcome.
Hi,
Several of the examples, like Valtio Tic Tac, work fine until you make an edit to the file that makes the call to
proxy
. The state is lost and everything resets.Try going to the above link and clicking a couple of times on the board. Then change any of the strings in the game's source. The game reloads and all the state information is lost. Obviously calling proxy again will cause everything to be reset, which is what will happen when hot reloading reloads the file.
I worked around the issue in my own project by just moving the state and call to proxy to a different file. Valtio drag-and-drop
You can drag around some objects and see their state updates. You can modify any of the sources except
globalcontext.js
and you won't lose state information. But just like in the other example, if you modifyglobalcontext.js
then all your state information is lost.Regular states produced by useState, useReducer, and createContext/useContext, are all able to retain their state information after a hot reload. Is there a way to do it with Valtio?
Thanks