-
-
Notifications
You must be signed in to change notification settings - Fork 617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
State resets with React Fast Refresh #1150
Comments
@Thisen Can you take a look? |
I'll take a look! |
@alan-nf, the Codesandbox doesn't seem to be using the |
No you're not blind, heh. It's using Create React App which uses react refresh under the covers from https://javascript.plainenglish.io/react-fast-refresh-the-new-react-hot-reloader-652c6645548c:
more specifically, it's using This is where it's hooked up: https://github.com/facebook/create-react-app/blob/main/packages/react-scripts/config/webpack.config.js#L35 |
And, if you grab the demo that can run locally, you can save a file edit and then see the network requests related to the react refresh update: |
I noticed that the hot update includes the atom declaration, presumably because it's in the same file as the component I'm editing. If I move that component to a separate file and edit it there, the jotai state stops resetting. That seems like totally reasonable behavior, and therefore isn't really reproducing the issue that caused me to file this bug report in the first place. In my codebase, editing files that do not have the jotai atom declarations are still resulting in the reset, so I guess I'm hitting a different bug. I'll try to get a simple repro of that issue. |
Ugh, I can't reproduce with vite, even when I perfectly match the versions of all the packages in my codebase. Here it is working just fine: https://stackblitz.com/edit/vitejs-vite-2tuyjr?file=src/App.tsx I'm going to try to figure out why this isn't working specifically in my codebase. I'll reopen this issue if I can get a correct repro of the problem. |
I'll look into this. @alan-nf can you add an updated reproduction? |
I'll work some more on getting a proper repo. Question: if I'm using vite and the |
fwiw I'm experiencing this issue as well w/ Vite 2.9.9, React 18, Jotai 1.7.2. steps to reproduce:
|
Yes, we only support React Refresh with the |
I'll look into |
@bstro have you tried to configure |
@Thisen yes, I copied the setup from the guide on my project and it has no effect. I can't use it in the StackBlitz example—for some reason adding the jotai babel plugin triggers this compilation error: |
Can you make a reproduction in Github repo that I can clone? |
Here's a reproduction on stackblitz of my state reset issue. The bug I'm seeing might be related to jotai's xstate module rather than @alan-nf's original issue. So far, I have only been able to reproduce the bug when using I hope this is helpful in some way. |
I'm not sure how much I can do without a reproduction using only the core modules. @dai-shi, any ideas if we could have a problem with |
I agree. We need someone help to reproduce it more simply.
|
Part of the problem is that we've all been discussing somewhat separate issues:
So put another way, the problems are:
If you want a simple repro, here is one. This repro:
This is the same code but the atom is moved to a separate file which lessens the problem with refresh (editing App.tsx does NOT reset the state, but editing state.ts still does reset the state) |
There's docs in Vite on how to configure Have you tried that? |
If I try to add the babel plugin on stackblitz: export default defineConfig({
plugins: [
react({
babel: {
plugins: ['jotai/babel/plugin-react-refresh'],
},
}),
],
}); Error:
Error Details
If I try to do the same thing locally (repo on github), I get the error:
Error Details
|
Duplicate of #1251. |
👍 I subscribed to that thread. When a fix is released I will see if there are any other blockers to getting Vite + Jotai + React Refresh playing nicely together and will report back here. Thanks! |
@alan-nf This is solved in 1.7.3 :) |
@Thisen I saw. I proofed it in the Stackblitz sandbox and it works perfectly there. I froze the sandbox and renamed it if you want to use it as an example/demo of vite + jotai working with react refresh. I have a note to myself to verify that this fixes things in my actual work project. I will report back as soon as I verify that. Thank you so much for fixing this. Should be a huge productivity booster. |
Glad to hear that! |
I'm seeing one more hiccup but I think it's to be expected. After a hot update, the atom update methods (result of Otherwise, this works great! |
Hm, not exactly sure, but if the atom reference is the same, the function should be the same. Can you create a minimal reproduction with codesandbox? |
Is stackblitz okay? if so, here you go: https://stackblitz.com/edit/jotai-vite-react-refresh-fretjv?file=src%2FApp.tsx Note that the atom reference is also changing as part of the hmr. I don't think this is a bug. In the stackblitz repro I demonstrate that it happens with setState as well (both the state and updater are new references). If you act on such changes you just have to be aware that the references might update in an hmr. I think that assuming otherwise is probably a sign of bad code anyway? |
Thanks.
"The updateInlineCount function was updated 12 times" Yeah, looks like working perfectly. |
Expected: when React Fast Refresh updates UI on save, jotai state should not be lost. E.g. a primitive counter atom like
const countAtom = atom(0)
should maintain it's value across fast refreshes.Actual: all jotai global state resets when React Fast Refresh updates the UI.
Codesandbox: https://codesandbox.io/s/jotai-react-refresh-bug-807nez?file=/src/Jotai.tsx
Local demo: https://github.com/alan-nf/jotai-react-refresh-issue
Instructions:
Copy
component to say something else. The counter will be reset back to 0.NOTE: related discussion about React Fast Refresh being broken in #1025. I cannot reproduce things being totally broken and I'm just seeing this reset issue.
The text was updated successfully, but these errors were encountered: