我在使用useRef带有样式组件的钩子时遇到了一些问题。
Linter 提醒我Object is possibly 'null'在 didMount 中useEffect。对此有什么想法吗?
ref在类组件中使用的,这是在 React 钩子之前使用它的唯一方法,innerRef道具是不是在风格组件的当前版本支持了。这是我的组件的示例片段:
import React, { useRef, useEffect } from 'react';
import styled from 'styled-components';
const StyledInput = styled.input`
background: transparent;
`
const MyForm = () => {
const inputRef = useRef(null);
useEffect(() => {
if (inputRef && inputRef.current) {
inputRef.current.focus(); //Object is possibly 'null'
}
}, []);
return (
<StyledInput ref={inputRef}/>
);
}
Run Code Online (Sandbox Code Playgroud) 一旦超出某个点,我需要禁用在html页面上向上滚动的功能.我还需要一种机制来返回到所述页面的顶部.
我已经尝试使用以下代码完成此操作,但它还没有为我工作,因为页面是响应的(因此点的位置可能会随窗口的大小而改变).
$(function() {
var scrollPoint = 200;
var scrolledPast = false;
$(window).scroll(function() {
$(window).scrollTop() > scrollPoint ? scrolledPast = true : '';
$(window).scrollTop() < scrollPoint && scrolledPast == true ?
$(window).scrollTop(scrollPoint) : '';
}).scroll();
});
Run Code Online (Sandbox Code Playgroud)
这可以用Javascript/JQuery完成吗?