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

useState使用更新函数方式更新数据不生效的问题 #162

Open
ckinmind opened this issue Aug 14, 2020 · 0 comments
Open

useState使用更新函数方式更新数据不生效的问题 #162

ckinmind opened this issue Aug 14, 2020 · 0 comments

Comments

@ckinmind
Copy link
Owner

问题描述

使用useState的更新函数去更新值,拿到最新的值之后,操作然后返回这个值,结果发现不更新,值是个二维数组,操作是删除其中的某一个数据

问题解决

官方文档里写了一段话React 使用 Object.is 比较算法 来比较 state, 这意味着useState是通过判断比较前后值是否是同一个对象来决定是否更新,而如果只是个二位数组或者对象,直接在原数据上操作,并且返回原数据,则比较的是同一个对象,当然就不更新了,所以react提倡state使用immutable数据

参考资料

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant