【发布时间】:2021-01-18 10:33:25
【问题描述】:
我正在尝试使用 react useState,useRef 与 Ionic 应用程序中的 typescript 挂钩,并且在尝试访问对象属性时不断出现愚蠢的 typescript 错误。
const [ matrix, updateMatrix ] = useState([]);
updateMatrix([['cell1', 'cell2', 'cell3'], ['cell4', 'cell5', 'cell6']])
在上面的代码中我得到了
“类型 'string' 不可分配给类型 'never'”
const basketEl = useRef(null);
if (basketEl.current) {
console.log(basketEl.current.getBoundingClientRect())
}
在上面的代码中,我得到 "Object is possible 'null'" for basketEl.current。即使我放了
if (basketEl.current !== null) {
错误仍然存在。 有趣的是,如果我不使用钩子并使用反应类,打字稿错误就会消失。修复ts错误需要花费大量时间,我放弃并开始使用类。也许我用错了钩子?有人同时使用钩子和打字稿有同样的问题吗?
【问题讨论】:
-
为什么你在使用 typescript 时不输入钩子?
标签: reactjs typescript react-hooks ionic4