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

feat: vue 版本的 useRequest 的结果能否支持解构 #77

Closed
GreatAuk opened this issue Aug 21, 2024 · 3 comments
Closed

feat: vue 版本的 useRequest 的结果能否支持解构 #77

GreatAuk opened this issue Aug 21, 2024 · 3 comments
Labels
enhancement New feature or request need reproduce not provide code split or repo

Comments

@GreatAuk
Copy link

GreatAuk commented Aug 21, 2024

<template>
  <block>
    <view>{{request.loading}}</view>
    <view>{{request.data}}</view>
    <view>{{request.error}}</view>
  </block>
</template>
<script>
  export default {
    setup() {
      // 由于要保证request内部的变量不失活。 这里简单的可以将request直接抛出
      const request = useRequest(getUsername);

      return {
        request,
      };
    },
  };
</script>

如果 data 是一个对象,那么使用起来会比较的 ugly, request.data.obj.a
reqeust 目前是一个 reactive 对象,如果解构,那么就会失去响应式。能否把 request 定义成一个普通的对象,里面的 data loading 等值定义为 ref, 这样解构后,响应式依然存在。这样使用上也会和 react 版的一致。

Description

Solution

可以参考 @tanstack/vue-query 的 API

// 其中  isPending, isError, data, error 都是 ref 类型的值
const { isPending, isError, data, error } = useQuery({
  queryKey: ['todos'],
  queryFn: getTodos,
})
@GreatAuk GreatAuk added the enhancement New feature or request label Aug 21, 2024
Copy link

welcome discuss in wechat:

@github-actions github-actions bot added the need reproduce not provide code split or repo label Aug 21, 2024
@innocces
Copy link
Owner

这个是因为内部需要用同样的 like react hooks 去完成一部分 hooksshims. 其中 vue.ver

useRef

const reactiveRef = reactive({ current: initialValue });

useMemo

return reactive(memoRef);

const vueRefResult = useMemo(

(因为 instance 本身是一个 useMemo)

const fetchInstance = useMemo(() => {

都使用了 reactive 的方式去设计的. 如果要改的话可能会涉及到其他 hooks 的回归。

@skyclouds2001
Copy link

可以自己用 toRefs() 把 reactive 对象转换成键值为 ref 的对象然后用解构赋值

@GreatAuk GreatAuk closed this as completed Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request need reproduce not provide code split or repo
Projects
None yet
Development

No branches or pull requests

3 participants