【发布时间】:2019-07-14 14:55:35
【问题描述】:
在我的组件中,我有以下代码可以滚动到页面底部:
const el = useRef(null);
useEffect(() => {
if (el.current === null) { }
else
el!.current!.scrollIntoView({ block: 'end', behavior: 'smooth' });
if (props.loading != true)
props.fetchFeedbackTaskPageData(submissionId);
}, [])
这个el 引用附加到一个div(在页面底部),如下所示:
<div id={'el'} ref={el}></div>
但是,我收到以下错误:
“从不”类型上不存在属性“scrollIntoView”。 TS2339
当我没有使用! 时,我收到了这个错误:
对象可能是“空”。 TS2531
我查看了很多关于这个问题的帖子,但是在使用 useRef 和 scrollIntoView 时没有看到如何处理这个问题。有什么想法吗?
【问题讨论】:
-
你是如何使用 ref 的?
-
@ravibagul91
elref 附加到一个 div(在页面底部):<div id={'el'} ref={el}></div>
标签: javascript reactjs typescript