解决react hook 在react版本16.13.1使用useEffect报错的问题

报错信息如下

TypeError: func.apply is not a function
    at HTMLUnknownElement.callCallback (http://localhost:1212/dist/renderer.dev.js:250583:14)
    at Object.invokeGuardedCallbackDev (http://localhost:1212/dist/renderer.dev.js:250633:16)
    at invokeGuardedCallback (http://localhost:1212/dist/renderer.dev.js:250690:31)
    at safelyCallDestroy (http://localhost:1212/dist/renderer.dev.js:267167:5)
    at commitUnmount (http://localhost:1212/dist/renderer.dev.js:267504:17)
    at commitNestedUnmounts (http://localhost:1212/dist/renderer.dev.js:267548:5)
    at unmountHostComponents (http://localhost:1212/dist/renderer.dev.js:267809:7)
    at commitDeletion (http://localhost:1212/dist/renderer.dev.js:267861:5)
    at commitAllHostEffects (http://localhost:1212/dist/renderer.dev.js:268602:11)
    at HTMLUnknownElement.callCallback (http://localhost:1212/dist/renderer.dev.js:250583:14)
    at Object.invokeGuardedCallbackDev (http://localhost:1212/dist/renderer.dev.js:250633:16)
    at invokeGuardedCallback (http://localhost:1212/dist/renderer.dev.js:250690:31)
    at commitRoot (http://localhost:1212/dist/renderer.dev.js:268826:7)
    at completeRoot (http://localhost:1212/dist/renderer.dev.js:270318:3)
    at performWorkOnRoot (http://localhost:1212/dist/renderer.dev.js:270247:9)
    at performWork (http://localhost:1212/dist/renderer.dev.js:270155:7)

 destroy is not a function
    at commitHookEffectList (http://localhost:1212/dist/renderer.dev.js:267241:11)
    at commitPassiveHookEffects (http://localhost:1212/dist/renderer.dev.js:267270:3)
    at HTMLUnknownElement.callCallback (http://localhost:1212/dist/renderer.dev.js:250583:14)
    at Object.invokeGuardedCallbackDev (http://localhost:1212/dist/renderer.dev.js:250633:16)
    at invokeGuardedCallback (http://localhost:1212/dist/renderer.dev.js:250690:31)
    at commitPassiveEffects (http://localhost:1212/dist/renderer.dev.js:268691:9)
    at wrapped (http://localhost:1212/dist/renderer.dev.js:298178:34)
    at flushFirstCallback (http://localhost:1212/dist/renderer.dev.js:298513:28)
    at flushWork (http://localhost:1212/dist/renderer.dev.js:298625:11)

在此版本下, 第一个参数不能用async进行声明,当然也不可以是一个异步函数,所以不支持以下写法。
useEffect(async () => {
}, []);

解析:useEffect hook 接受的第一个参数可以返回一个清楚副作用的函数。所以当使用async声明后,返回值为一个Promise对象。在此版本中,并未对这种情况进行处理,所以会造成以上报错。

react版本大于17可以支持。

你可能感兴趣的:(解决react hook 在react版本16.13.1使用useEffect报错的问题)