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

[Need Help]runAsync的返回值与data是指向同一个响应式对象吗? #229

Closed
Akemi-Madoka opened this issue Mar 11, 2024 · 1 comment
Labels
question Further information is requested

Comments

@Akemi-Madoka
Copy link

Akemi-Madoka commented Mar 11, 2024

const { data, runAsync } = useRequest<R, P>(Service, Options);

runAsync(Options).then((asyData))=>{
asyData.item = 'test'
})

这里的data与asyData是同一个响应式对象吗?
发现修改asyData时,会导致data一起被修改。

当将返回值传输给其他组件时,容易出现忘记响应式绑定的问题,需要手动加入toValue()

这个情况也导致mutate这个函数变得很奇怪。
也许asyData应该返回非响应式对象?但会导致兼容性问题?
但至少建议在文档这段加入,提示其响应式性质。

@Akemi-Madoka Akemi-Madoka added the question Further information is requested label Mar 11, 2024
@John60676
Copy link
Member

这里的data与asyData是同一个响应式对象吗?

data 是响应式对象,asyData 是普通对象

发现修改asyData时,会导致data一起被修改。

修改 asyData 的结果,data 会跟着变是因为他们共用一个引用

如果要对 data 进行修改的,建议使用 mutate
如果要对 asyData 进行处理的,建议深拷贝后再修改

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants