Skip to content
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

useRequest在严格模式StrictMode下,缓存失效 #2526

Open
fred8617 opened this issue Apr 21, 2024 · 9 comments
Open

useRequest在严格模式StrictMode下,缓存失效 #2526

fred8617 opened this issue Apr 21, 2024 · 9 comments
Labels
bug Something isn't working

Comments

@fred8617
Copy link

fred8617 commented Apr 21, 2024

Preview

@zhangmo8
Copy link

Maybe you need to set it to not expire or staleTime

@zhangmo8
Copy link

{ cacheKey: "cacheKey-share", staleTime: -1 }

@fred8617
Copy link
Author

Maybe you need to set it to not expire or staleTime

it's wried. I used official example in the Preview, why to set staleTime in strict mode? @zhangmo8

@zhangmo8
Copy link

I don't. But it's work.

@fred8617
Copy link
Author

@zhangmo8 actually, we are not on the same page. I need refresh the datas, but if I set staleTime to -1, the refresh is invalid.

@zhangmo8
Copy link

ok. 我知道啦...英文描述比较麻烦...
它的行为是对的,或者说在预料之中的...
你设置的请求是3s,在这3s内,数据是已经被缓存下来的,所以当点击【更新】按钮的一瞬间,它会直接读取缓存的数据,并在请求完成(3s)后更新数据,我理解这是符合预期的。

你的问题其实是在于,非严格模式下,它两个数据是同步的,而严格模式下并没有同步更新,我认为它可能是react的render策略引起的问题

语言描述比较苍白,为此,我录制了一个视频来描述这个行为。并且更改了请求时间为5s,以便你能够更好的看到它读取缓存的数据

Screen.Recording.2024-04-24.at.11.00.37.mov

另外附上链接 Preview

@fred8617
Copy link
Author

你的问题其实是在于,非严格模式下,它两个数据是同步的,而严格模式下并没有同步更新,我认为它可能是react的render策略引起的问题

OK got it, 其实我想说的就是这个问题,如果开启StrictMode,就只能手动去refresh了

@zhangmo8
Copy link

zhangmo8 commented Apr 24, 2024

ok. 我知道啦...英文描述比较麻烦... 它的行为是对的,或者说在预料之中的... 你设置的请求是3s,在这3s内,数据是已经被缓存下来的,所以当点击【更新】按钮的一瞬间,它会直接读取缓存的数据,并在请求完成(3s)后更新数据,我理解这是符合预期的。

你的问题其实是在于,非严格模式下,它两个数据是同步的,而严格模式下并没有同步更新,我认为它可能是react的render策略引起的问题

语言描述比较苍白,为此,我录制了一个视频来描述这个行为。并且更改了请求时间为5s,以便你能够更好的看到它读取缓存的数据

Screen.Recording.2024-04-24.at.11.00.37.mov
另外附上链接 Preview

所以,感觉是应该去给 React 提交 issues,或者采用 抽离状态,memo等方式阻止它的更新,来保持和严格模式下的表现一致,以便你在开发环境的表现行为和打包后的表现行为一致。

当然,如果你是拉磨,我更推荐去除严格模式

@liuyib
Copy link
Collaborator

liuyib commented Apr 24, 2024

严格模式下的行为,确实和官网预期的不一样。看来严格模式下的单测有点问题

@liuyib liuyib added the bug Something isn't working label Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants