pmndrs / valtio

🧙 Valtio makes proxy-state simple for React and Vanilla
https://valtio.dev
MIT License
9.16k stars 257 forks source link

Can proxy be durable to a hot reload? #36

Closed Wombat42 closed 3 years ago

Wombat42 commented 4 years ago

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 modify globalcontext.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

Aslemammad commented 4 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.

dai-shi commented 4 years ago

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.

thelinuxlich commented 3 years ago

Isn't Vue model a bit similar?

thelinuxlich commented 3 years ago

I'm using Valtio with Vite bundler and not even HMR works

dai-shi commented 3 years ago

Never used vite bundler. Would you be able to create a small repro that we can try on our end?

thelinuxlich commented 3 years ago

FYI Vite works well with Valtio now

dai-shi commented 3 years ago

Let me close this issue. Basically, hot reload works as expected if state is defined in a different file. Suggestions are welcome.