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

useResetState 并不能准确记录 第一次的初始值 #2306

Closed
kazura233 opened this issue Aug 29, 2023 · 4 comments · Fixed by #2562
Closed

useResetState 并不能准确记录 第一次的初始值 #2306

kazura233 opened this issue Aug 29, 2023 · 4 comments · Fixed by #2562

Comments

@kazura233
Copy link

useResetState 并不能准确记录 第一次的初始值

import { useResetState } from 'ahooks'

const [spawnPoint, setSpawnPoint, resetSpawnPoint] = useResetState({
    spawnPoint: Math.random(),
})

resetSpawnPoint()

期望结果,reset应该重置state为第一次渲染时,随机产生的值。
现在,reset会重设为 最近一次重新渲染时,产生的值。

@liuyib
Copy link
Collaborator

liuyib commented Aug 29, 2023

目前是有这个问题。但是能否说下,“初始值为随机值,并且 reset 时还需要准确还原为初始随机值”的业务场景吗?

@kazura233
Copy link
Author

目前是有这个问题。但是能否说下,“初始恢复随机值,并且重置时还需要还原准确为初始随机值”的业务场景吗?

在业务中,目前没有这个场景呢。
在项目中,我自己写了一个useResetState,后来发现咱们ahook有这个。
就打算用ahook的代替我的,跑了一遍我的单测,发现了这个问题,就来问问,是否设计如此。

@liuyib
Copy link
Collaborator

liuyib commented Aug 30, 2023

最初设计应该没有考虑这种随机值的初始值,但感觉确实没有使用场景,不过也可以修复下,可以来个 PR,或者后面我会处理

@hemengke1997
Copy link
Contributor

当初没有考虑到随机数的情况,已提PR,麻烦作者大大review一下啦😬

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