在 onDragStop 事件中获取 MUI Slider 值 [英] Get MUI Slider value in onDragStop event
问题描述
我想在我的 React 应用程序中使用 MUI Slider
来触发事件 onDragStop
而不是 onChange
(这样事件触发次数就会减少).但是,documentation 表明 onDragStop
函数签名只有mouseevent: function(event: object) =>无效
.因此,以下适用于 onChange
:
I want to fire an event onDragStop
rather than onChange
using a MUI Slider
in my React app (so that the event fires fewer times). However, the documentation indicates that the onDragStop
function signature only has the mouseevent: function(event: object) => void
. So, the following works with onChange
:
<Slider onChange={ (e, val) => this.props.update(e, control.id, val) } />
但是,这个事件没有第二个参数val
:
However, this event doesn't have a second parameter val
:
<Slider onDragStop={ (e, val) => this.props.update(e, control.id, val) } />
如何在 onDragStop
函数中获取 Slider 的当前值?请注意,我无法使用 this
,因为它指的是父组件.
How can I get the current value of the Slider in the onDragStop
function? Note, I'm unable to use this
, as it refers to the parent component.
推荐答案
也遇到了这个问题!如果您在类中使用组件,请同时使用两个回调:
Also ran into this problem! If you use a component inside a class, use both callbacks:
<Slider onChange={ (e, val) => this.val = val }
onDragStop={ (e) => this.props.update(e, control.id, this.val)
/>
这篇关于在 onDragStop 事件中获取 MUI Slider 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!