鑒于這個非常簡單的組件:
const InputElement => React.forwardRef((props:any, ref) => {
const handleRef = React.useRef<HTMLInputElement|undefined>()
React.useImperativeHandle(ref, () => ({
setChecked(checked:boolean) {
if (handleRef.current) {
handleRef.current.checked = checked;
}
}
}), []);
return (
<input ref={ handleRef } type="checkbox" /> {/* <-- error here */}
)
})
我有這個錯誤:
Type 'MutableRefObject<HTMLInputElement | undefined>' is not assignable to type 'LegacyRef<HTMLInputElement> | undefined'.
Type 'MutableRefObject<HTMLInputElement | undefined>' is not assignable to type 'RefObject<HTMLInputElement>'.
Types of property 'current' are incompatible.
Type 'HTMLInputElement | undefined' is not assignable to type 'HTMLInputElement | null'.
Type 'undefined' is not assignable to type 'HTMLInputElement | null'.ts(2322)
這是什么意思?如何修復此錯誤?
uj5u.com熱心網友回復:
您應該將初始值傳遞給useRef鉤子。| undefined也不需要添加:
React.useRef<HTMLInputElement>(null)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/526253.html
標籤:反应打字稿
上一篇:從串列中洗掉上傳的檔案
