reactjs - react-day-picker ref 当前 scrollIntoView 不可访问
问题描述
我正在使用 react-day-picker。我想在单击输入后滚动到“日历”-DayPicker 组件(问题是当页面滚动时,日历不在视口中。我尝试了这种方法(简化):
import React from 'react';
import DayPickerInput from 'react-day-picker/DayPickerInput';
class DatePicker extends React.Component {
constructor(props) {
super(props);
this.fromDayPickerRef = React.createRef();
this.toDayPickerRef = React.createRef();
this.handleOnDayPickerShow = ref => { this[ref].current.scrollIntoView() }
}
render() {
const {from, to} = this.state;
const modifiers = {start: from, end: to};
return (
<div className="InputFromTo">
<DayPickerInput
...
dayPickerProps={{
...
ref: this.fromDayPickerRef,
}}
...
onDayPickerShow={() => this.handleOnDayPickerShow("fromDayPickerRef")}
/><br/>
<span className="InputFromTo-to">
<DayPickerInput
...
dayPickerProps={{
...
ref: this.toDayPickerRef,
}}
...
onDayPickerShow={() => this.handleOnDayPickerShow("toDayPickerRef")}
/>
</span>
</div>
}
}
我能够访问.current但没有 HTML DOM 道具(例如 offSetTop)或函数。有什么猜测吗?
解决方案
我想到了。您不能像这样引用函数组件(可能使用 forwardRef)。但是稍后会将引用传递给 DOM 元素。您需要通过以下方式进行操作:
dayPickerProps={{
...
ref: el => {this.fromDayPickerRef = el},
}}
推荐阅读
- javascript - JSON:无法读取未定义的属性“键值”
- python - 有没有更有效的方法来感应鼠标在一堆点上?
- rust - 无法使用 postgres 功能编译 Rust SQLx
- r - R中NA值的线性插值
- uwp - UWP CalendarDatePicker GetTemplate子调用
- r - 使用 R 中未来值的条件覆盖数据框中的值
- php - 从列表中删除 PHP 单词
- javascript - ReactJS 错误:“__”未定义 & 期望赋值或函数调用,而是看到了一个表达式?
- python - 修复包含 Scrollable FigureCanvasTkAgg 的 Canvas 的大小
- typescript - .vue 文件中的 TypeScript 类型断言似乎让 ESLint 感到困惑