我使用這段代碼:
const clearTimer = (e: any) => {
setTimer(<div></div>);
if (Ref.current) clearInterval(Ref.current);
startTimer(e);
const id = setInterval(() => {
startTimer(e);
}, 1000)
Ref.current = id;
}
因為Ref.current = id;我收到此錯誤:
(property) MutableRefObject<null>.current: null
型別“計時器”不可分配給型別“空”。
這些行并沒有解決我的問題:
Ref.current = id.toString();
Ref.current = id ?? '';
uj5u.com熱心網友回復:
與 Typescript 一起使用useRef時,您應該提供一個通用引數來指定所持有值的預期型別。
在你的情況下:
const Ref = useRef<Timer>(null);
然后Ref.current可以賦值給 的結果setInterval。
有關更多資訊,請參閱本文:https ://linguinecode.com/post/how-to-use-react-useref-with-typescript
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/416679.html
標籤:
