简体   繁体   English

'() 类型的参数 =&gt; () =&gt; Promise<void> ' 不可分配给“EffectCallback”类型的参数</void>

[英]Argument of type '() => () => Promise<void>' is not assignable to parameter of type 'EffectCallback'

My code is:我的代码是:

    useEffect(() => {
        document.querySelector('body').style['background-color'] = 'rgba(173, 232, 244,0.2)';
        window.$crisp.push(['do', 'chat:hide']);

        return async () => {
            window.$crisp.push(['do', 'chat:show']);
            document.querySelector('body').style['background-color'] = '#ffffff';
            if (router.query.id) {
                const resDoc = await firebase.firestore().doc(`documents/${router.query.id}`).get();
                if (resDoc.exists) {
                    await clearDocPrediction(router.query.id);
                }
            }
        };
    }, []);

Now the reason I'm returning is because I believe that it performs function cleanup on unload, so I need to keep that.现在我回来的原因是因为我相信它会在卸载时执行 function 清理,所以我需要保留它。 This is some sort of build error that I'm not quite sure how to resolve.这是某种构建错误,我不太确定如何解决。 Any help would be greatly appreciated.任何帮助将不胜感激。

The useEffect callback should have return type void. useEffect回调应该有返回类型 void。 So you can't return a Promise:所以你不能返回 Promise:

useEffect(() => {
        document.querySelector('body').style['background-color'] = 'rgba(173, 232, 244,0.2)';
        window.$crisp.push(['do', 'chat:hide']);

        window.$crisp.push(['do', 'chat:show']);
        document.querySelector('body').style['background-color'] = '#ffffff';
        if (router.query.id) {
            firebase.firestore()
                .doc(`documents/${router.query.id}`)
                .get()
                .then((resDoc) => {
                    if (resDoc.exists) {
                        clearDocPrediction(router.query.id);
                    }
                });
        }
}, []);

Effect's callback function should return void , eg:效果的回调 function 应该返回void ,例如:

useEffect(() => { // callback; it returns void
  console.log("Hi")
}, [])

Or或者

It should return a " cleanup " function of type () => void , eg:它应该返回() => void类型的“清理” function ,例如:

useEffect(() => {
  console.log("Hi")
  return () => { // cleanup function of type : () => void
    console.log("Cleanup")
  }
}, [])

You are getting the error because the cleanup function in your code is not of type () => void , but of type () => Promise<void> , eg:您收到错误是因为您的代码中的清理 function 不是() => void类型,而是() => Promise<void>类型,例如:

Argument of type '() => () => Promise' is not assignable to parameter of type 'EffectCallback'. '() => () => Promise' 类型的参数不可分配给'EffectCallback' 类型的参数。 Type '() => Promise' is not assignable to type 'void |类型 '() => Promise' 不可分配给类型 'void | Destructor'.破坏者”。

useEffect(() => {
  console.log("Hi")
  return async () => { // WRONG
    console.log("Cleanup")
  }
}, [])

Hence, here is how you can refactor your "cleanup" function to fix it:因此,您可以通过以下方式重构“清理” function 来修复它:

return () => { // Remove async from here
  const clear = async () => { // Create a new async function: clear
    // write your cleanup code here
  };
  clear() // Call the clear() function
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现错误:'() =&gt; () =&gt; boolean' 类型的参数不可分配给'EffectCallback' 类型的参数 - getting error : Argument of type '() => () => boolean' is not assignable to parameter of type 'EffectCallback' &#39;(dispatch: Dispatch 类型的参数<ShopDispatchTypes> ) =&gt; 承诺<void> &#39; 不可分配给“AnyAction”类型的参数 - Argument of type '(dispatch: Dispatch<ShopDispatchTypes>) => Promise<void>' is not assignable to parameter of type 'AnyAction' 类型参数 (key: string, id: string, pagination: number) =&gt; Promise<void> 不能分配给布尔类型的参数 - Argument of type (key: string, id: string, pagination: number) => Promise<void> is not assignable to parameter of type Boolean useEffect 清理函数打字稿错误:不可分配给“EffectCallback”类型的参数 - useEffect cleanup function Typescript error: not assignable to parameter of type 'EffectCallback' 调度问题“类型参数&#39;(调度:调度<T> ) =&gt; 承诺<void> &#39; 不能分配给类型为 &#39;T&#39; 的参数。” - Problem with dispatch "Argument of type '(dispatch: Dispatch<T>) => Promise<void>' is not assignable to parameter of type 'T'." 这个错误是什么意思? &#39;(dispatch: Dispatch 类型的参数<IAuthType> ) =&gt; 承诺<void> &#39; 不可分配给“AnyAction”类型的参数 - What is this err means? Argument of type '(dispatch: Dispatch<IAuthType>) => Promise<void>' is not assignable to parameter of type 'AnyAction' “(事件:字符串)=&gt; void”类型的参数不可分配给“EventListenerOrEventListenerObject”类型的参数 - Argument of type '(event: string) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject Promise 类型的参数<memberentityvm[]>不可分配给 SetStateAction 类型的参数<memberentityvm[]></memberentityvm[]></memberentityvm[]> - Argument of type Promise<MemberEntityVM[]> is not assignable to parameter of type SetStateAction<MemberEntityVM[]> '(dispatch: Dispatch) =&gt; void' 类型的参数不可分配给 'AnyAction' 类型的参数 - Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction' “AsyncThunkAction”类型的参数<any, void, {}> ' 不可分配给 'AnyAction' 类型的参数</any,> - Argument of type 'AsyncThunkAction<any, void, {}>' is not assignable to parameter of type 'AnyAction'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM