我有一个组件,它会随着鼠标的拖动而改变网格上的高亮显示。
public handleDragMove = (e: any) => {
if (this.props.highlightActive) {
const rindex: number = e.currentTarget.parentNode.rowIndex;
const cindex: number = e.currentTarget.cellIndex;
const grid = {
rowIndex: rindex,
cellIndex: cindex,
};
if (rindex !== this.state.grid.rowIndex && cindex !== this.state.grid.cellIndex){
if (e.target.style.backgroundColor === 'blue') {
e.target.style.backgroundColor = 'red';
}
else {
e.target.style.backgroundColor = 'blue';
}
this.setState({
gridData: gridData
});
}
}
}
}
我尝试使用setState
,但是它在任何地方更新的速度都不够快。到状态更新时,由于发生了拖动事件,因此有20个调用来更新状态。因此,当鼠标在细胞上移动时,细胞会快速闪烁。
在这种情况下,使用简单地以同步方式更新坐标的变量是否是反模式?我尝试过使用回调的方法,但似乎无法获得异步解决方案。