reactjs - 获取与窗口相关的元素顶部偏移量
问题描述
我正在尝试实现将返回元素的 offsetTop 的钩子。这是需要将一些样式应用到它粘在顶部的标题。
import { useState, useEffect } from 'react';
interface ElementPosition {
left: number | undefined;
right: number | undefined;
top: number | undefined;
bottom: number | undefined;
}
export const useElementPosition = (ref: any): ElementPosition => {
const [position, setPosition] = useState<ElementPosition>({
left: undefined,
right: undefined,
top: undefined,
bottom: undefined,
});
useEffect(() => {
if (ref.current) {
setPosition({
top: ref.current.offsetTop,
bottom: ref.current.offsetBottom,
left: ref.current.offsetLeft,
right: ref.current.offsetRight,
});
}
}, [ref.current]);
return position;
};
但现在我记得ref.current
更改不会触发 useEffect 所以我只能获得初始偏移量。有没有办法以可重用的方式做到这一点hooks
?
解决方案
对于这种情况,您可以改用回调ref
。ref
当附加到新节点时,将调用回调。
export const useElementPosition = () => {
const [position, setPosition] = useState<ElementPosition>({
left: undefined,
right: undefined,
top: undefined,
bottom: undefined
});
const ref = useCallback((node) => {
if (node !== null) {
setPosition({
top: node.offsetTop,
bottom: node.offsetBottom,
left: node.offsetLeft,
right: node.offsetRight
});
}
}, []);
return [position, ref];
}
在这种情况下,不是将 a 传递ref
给自定义钩子,而是ref
从它返回要使用的。
const PositionExample = () => {
const [position, ref] = useElementPosition();
console.log(position.top);
return (
<h1 ref={ref}>Hello world!</h1>
);
};
有关更多详细信息,请查看官方文档:React Hooks - How can I measure a DOM node? .
推荐阅读
- javascript - 如何获取 AnimatedInterpolation 的值
- linux - 如果 ping 成功,Linux shell (sh) CLI 测试
- java - Kotlin:将日期字符串转换为 ISO 字符串
- r - 通过保持指标变量从年份数据创建月度数据
- oracle - 将 HIVE 数据库复制到 Oracle
- wpf - 高度和宽度改变后,WPF MVVM在屏幕上重新居中应用程序?
- tfs - TFS 释放门不触发
- groovy - wiremock-groovy 库语法
- cors - Svelte API 代理服务器
- stm32 - 如何进行 USART 通信 请告诉我进行 usart 通信应注意的一切