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

Uncaught TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function #2027

Closed
ctocto opened this issue Jan 11, 2023 · 14 comments

Comments

@ctocto
Copy link

ctocto commented Jan 11, 2023

"ahooks": "3.7.4",

错误信息:

Uncaught TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function
    at useRequestImplement.js:34:21
    at index.js:11:45
    at invokePassiveEffectCreate (react-dom.development.js:23487:20)
    at HTMLUnknownElement.callCallback2 (react-dom.development.js:3945:14)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:16)
    at invokeGuardedCallback (react-dom.development.js:4056:31)
    at flushPassiveEffectsImpl (react-dom.development.js:23574:9)
    at unstable_runWithPriority (scheduler.development.js:468:12)
    at runWithPriority$1 (react-dom.development.js:11276:10)
    at flushPassiveEffects (react-dom.development.js:23447:14)

突然今天构建就报上面的错误,排查发现是因为3.7.4版本与之前使用3.7.0版本,在defaultParams参数处理有改变:

// 3.7.0
useMount(function () {
    if (!manual) {
        // useCachePlugin can set fetchInstance.state.params from cache when init
        var params = fetchInstance.state.params || options.defaultParams || []; // @ts-ignore

        fetchInstance.run.apply(fetchInstance, __spread(params));
    }
});
// 3.7.4
useMount(() => {
    if (!manual) {
        // useCachePlugin can set fetchInstance.state.params from cache when init
        const params = fetchInstance.state.params || options.defaultParams || [];
        // @ts-ignore
        fetchInstance.run(...params);
    }
});

可以发现在 3.7.0版本使用了__spread函数对参数进行了处理,而3.7.4版本去掉了这个处理。在我以前的代码中 defaultParams 传的是一个对象不是数组,如:

const {
    data,
} = useRequest(getUsername, {
    defaultParams: {page: 1, pageSize: 10},
});

次用法在3.7.4版本上是报错的。

ahooks这个修改算不算是一个破坏性的更新?

@crazylxr
Copy link
Collaborator

emm,我看 3.7.0 也还是 fetchInstance.run(...params); 呢。https://github.com/alibaba/hooks/blob/v3.7.0/packages/hooks/src/useRequest/src/useRequestImplement.ts

@crazylxr
Copy link
Collaborator

这个改动我看了下提交记录,应该是 v2 -> v3 的时候改的

@thinkingc
Copy link

这就奇怪了,为啥node_modules里的3.7.0代码还是用__spread呢?

image
image

@crazylxr
Copy link
Collaborator

crazylxr commented Feb 20, 2023

这就奇怪了,为啥node_modules里的3.7.0代码还是用__spread呢?

image image

你这个是打包后的代码,不是源码哦。我看下是不是 升级了编译相关的库

@athrunsaga
Copy link

遇到同样的问题,排查了半天才发现,目前只能先把版本限定在 3.7.0

@liuyib
Copy link
Collaborator

liuyib commented Mar 13, 2023

这就奇怪了,为啥node_modules里的3.7.0代码还是用__spread呢?

image image

删除 node_modules,重新 install 试试呢?麻烦试下 不用试了我这边复现了

@liuyib
Copy link
Collaborator

liuyib commented Mar 13, 2023

遇到同样的问题,排查了半天才发现,目前只能先把版本限定在 3.7.0

删除 node_modules,重新 install 试试呢?麻烦试下 不用试了我这边复现了

@liuyib
Copy link
Collaborator

liuyib commented Mar 13, 2023

我这边已经确认,你反馈的打包产物不同是确实存在的。但是你不应该这样用啊:

image

类型检测都没法通过吧?
@ctocto

@liuyib
Copy link
Collaborator

liuyib commented Mar 13, 2023

我这边尝试的你的那种用法,没法通过类型检测:

image

image

@liuyib
Copy link
Collaborator

liuyib commented Mar 13, 2023

这个问题我们会进一步定位

@thinkingc
Copy link

我这边尝试的你的那种用法,没法通过类型检测:

image image

我项目不是用ts写的,检测不到。

@liuyib
Copy link
Collaborator

liuyib commented Mar 13, 2023

这个参数,从 v2 升级到 v3 的时候就只支持数组了,建议你把参数改为数组解决下问题。这个问题我们会进一步深度定位 @thinkingc

@thinkingc
Copy link

这个参数,从 v2 升级到 v3 的时候就只支持数组了,建议你把参数改为数组解决下问题。这个问题我们会进一步深度定位 @thinkingc

目前锁定版本号3.7.0解决的

@crazylxr
Copy link
Collaborator

crazylxr commented Mar 14, 2023

这个问题跟 #1939 是一样的, 原因和处理方式我都已经给出了,这个 issue 就先 close 了

大家还是得按照文档给的类型来传,不要以运行能用 就行,文档里面写的,我们就是会负责的,如果不按照文档里面要求的进行传递,容易出现问题。

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

No branches or pull requests

5 participants