【发布时间】:2019-10-17 23:13:10
【问题描述】:
我正在开发一个简单的组件“BackToTop”
const BackToTop: React.FC = () => {
const bttEl = useRef(null);
function scrollHandler(): void {
var bttHtmlEl: HTMLElement | null = bttEl.current;
// console.log(bttHtmlEl); Element OK!
if ( bttHtmlEl ) {
window.pageYOffset > 50 ? bttHtmlEl.classList.remove('is-hide'): bttHtmlEl.classList.add('is-hide');
} else {
console.error('BackToTop is null.');
}
}
useEffect( ()=>{
window.addEventListener('scroll', scrollHandler);
return ()=>{
window.removeEventListener('scroll', scrollHandler);
}
}
)
src/components/BackToTop.tsx:54:43 - 错误 TS2339:“从不”类型上不存在属性“classList”。
54 window.pageYOffset > 50 ? bttHtmlEl.classList.remove('is-hide'): bttHtmlEl.classList.add('is-hide'); ~~~~~~~~~
src/components/BackToTop.tsx:54:82 - 错误 TS2339:“从不”类型上不存在属性“classList”。
54 window.pageYOffset > 50 ? bttHtmlEl.classList.remove('is-hide'): bttHtmlEl.classList.add('is-hide'); ~~~~~~~~~
【问题讨论】:
标签: reactjs typescript