我正在开发一个具有可变时间范围的日程/日历应用程序。要显示当前时间的一行,并显示已完成约会的块,我需要计算给定时间范围内每分钟对应的像素数。
例如:如果议程从早上7点开始,到下午5点结束,总行程是10小时。假设日历的主体有1000个像素。这意味着每小时代表100个像素,每分钟代表1,66个像素。
如果现在是下午3点的话。我们距离议程开始还有480分钟。这意味着显示当前时间的线应该在日历正文顶部的796,8像素(480 * 1,66)。
没有问题的计算,但在获得高度的议程机构。我正在考虑用React来获得高度,但我得到了一个错误: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>
);
}
}发布于 2021-04-01 15:28:38
如果使用react-redux并将组件包装为connect函数,则需要传递第四个参数,即像这样的forwardRef。
connect(mapStateToProps, mapDispatchToProps, null, {forwardRef: true})我希望这会有所帮助。
https://stackoverflow.com/questions/55248483
复制相似问题