![](/img/trans.png)
[英]Argument of type '() => JQuery' is not assignable to parameter of type '() => boolean'
[英]getting error : Argument of type '() => () => boolean' is not assignable to parameter of type 'EffectCallback'
我是 typescript 的新手,当我尝试在反应中使用useEffect
中的 useEffect 时出现错误, Argument of type '() => () => boolean' is not assignable to parameter of type 'EffectCallback'.
,任何人都可以帮助我为什么会收到此错误? 我在这里放了我的代码,任何帮助将不胜感激,
const useIsMounted = () => {
const isMounted = React.useRef(false);
React.useEffect(() => {
isMounted.current = true;
return () => isMounted.current = false;
}, []);
return isMounted;
};
useEffect
( EffectCallback
类型)的 function 应该返回void
或() => void | undefined
() => void | undefined
。
function useEffect(effect: EffectCallback, deps?: DependencyList): void;
type EffectCallback = () => (void | (() => void | undefined));
在您的情况下,您返回void => boolean
:
// void => boolean
return () => (isMounted.current = false);
要修复它,请将 scope 添加到清洁 function 的语句中:
const useIsMounted = () => {
const isMounted = React.useRef(false);
React.useEffect(() => {
isMounted.current = true;
return () => {
isMounted.current = false;
};
}, []);
return isMounted;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.