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

fix(createUseStorageState): 修复setState不能拿到上次修改的值 #2390

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Damon0820
Copy link

@Damon0820 Damon0820 commented Dec 5, 2023

[中文版模板 / Chinese template]

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

fix: #2389

💡 Background and solution

见: #2389

📝 Changelog

Language Changelog
🇺🇸 English
🇨🇳 Chinese

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@CLAassistant
Copy link

CLAassistant commented Dec 5, 2023

CLA assistant check
All committers have signed the CLA.

@liuyib
Copy link
Collaborator

liuyib commented Dec 6, 2023

这个是已知问题,改变了原有行为,属于破坏性更改,v4 再做。

#2082 这个 PR 也是处理的这个问题,我验证了下,和这个 PR 修复结果一样,都正常,见 demo:https://stackblitz.com/edit/stackblitz-starters-nppnxu?file=src%2FApp.tsx

@liuyib
Copy link
Collaborator

liuyib commented Dec 6, 2023

统一在 #2082 中处理

@liuyib liuyib closed this Dec 6, 2023
@Damon0820
Copy link
Author

Damon0820 commented Dec 6, 2023

看了下 #2082 的修复方案,解决了上述行为和setState不一致的问题👍。但是存在一个细节不够完美,状态值和storage的值没有保持同步。这是因为使用 useEffect( storage.setItem(state), [state] ) 去同步 state -> storage 导致的。这个问题在此pr的解决方案中不会出现。
可查看此demo对比了两种方案: https://stackblitz.com/edit/stackblitz-starters-zmdjte?file=src%2FlocalStorage.ts
企业微信截图_5df04934-e33f-433a-b89d-8e7ffb12af5f
企业微信截图_f38529e8-2b1d-480e-b6d4-c97fa1a6cf3e

@liuyib
Copy link
Collaborator

liuyib commented Dec 7, 2023

好的,感谢指出问题,v4 的时候会综合考虑这两个 PR

@liuyib liuyib reopened this Dec 7, 2023
@liuyib liuyib added the v4 label Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

【bug】createUseStorageState连续多次setState(pre => xx)的表现和 useState 的不一致
3 participants