【发布时间】:2019-08-10 10:07:51
【问题描述】:
我正在开发具有可变时间范围的议程/日历应用程序。要显示当前时间的一条线并显示已进行的约会的块,我需要计算给定时间范围内与一分钟对应的像素数。
例如:如果议程从早上 7 点开始,到下午 5 点结束,则总范围为 10 小时。假设日历主体的高度为 1000 像素。这意味着每小时代表 100 像素,每分钟代表 1,66 像素。
如果当前时间是下午 3 点。距离议程开始还有 480 分钟。这意味着显示当前时间的行应位于距日历主体顶部 796,8 像素 (480 * 1,66) 处。
计算没有问题,但获得了议程主体的高度。我正在考虑使用 React Ref 来获取高度,但出现错误:ref.current is null
下面是一些代码:
class Calendar extends Component {
calendarBodyRef = React.createRef();
displayCurrentTimeLine = () => {
const bodyHeight = this.calendarBodyRef.current.clientHeight; // current is null
}
render() {
return (
<table>
<thead>{this.displayHeader()}</thead>
<tbody ref={this.calendarBodyRef}>
{this.displayBody()}
{this.displayCurrentTimeLine()}
</tbody>
</table>
);
}
}
【问题讨论】: