Tôi biết câu trả lời được chấp nhận là rất tốt nhưng đối với những ai đang tìm kiếm một di chuột như cảm giác bạn có thể sử dụng setTimeouttrên mouseovervà lưu các xử lý trong một bản đồ (danh sách nói id let để setTimeout Xử lý). Khi mouseoverxóa tay cầm khỏi setTimeout và xóa nó khỏi bản đồ
onMouseOver={() => this.onMouseOver(someId)}
onMouseOut={() => this.onMouseOut(someId)
Và thực hiện bản đồ như sau:
onMouseOver(listId: string) {
this.setState({
... // whatever
});
const handle = setTimeout(() => {
scrollPreviewToComponentId(listId);
}, 1000); // Replace 1000ms with any time you feel is good enough for your hover action
this.hoverHandleMap[listId] = handle;
}
onMouseOut(listId: string) {
this.setState({
... // whatever
});
const handle = this.hoverHandleMap[listId];
clearTimeout(handle);
delete this.hoverHandleMap[listId];
}
Và bản đồ giống như vậy,
hoverHandleMap: { [listId: string]: NodeJS.Timeout } = {};
Tôi thích hơn onMouseOvervà onMouseOutvì nó cũng áp dụng cho tất cả trẻ em trong HTMLElement. Nếu điều này không bắt buộc, bạn có thể sử dụng onMouseEntervà onMouseLeavetương ứng.