我正在使用useRef
但addEventListener
不起作用,这里有什么问题吗?
父组件:
const parent = () => {
const el = useRef()
const element = el.current && el.current
useEffect(() => {
if (element) {
const scrollHandler = e => {
console.log(e);
};
element.addEventListener("scroll", scrollHandler);
return () => {
element.removeEventListener("scroll", scrollHandler);
};
}
}, [element]);
return (
<Child ref={el} />
)
}
子组件:
const child = React.forwardRef((props, ref) => {
return (
<div ref={ref}>something...</div>
)
})