https://www.npmjs.com/package/nouislider-react 无法将值插入滑块
class KeyboardSlider extends React.Component {
state = { ref: null };
changeByRef = () => {
const { ref } = this.state;
if (ref && ref.noUiSlider) {
ref.noUiSlider.set(20);
}
};
render() {
return (
<div>
<button onClick={this.changeByRef}>Change with ref</button>
<Nouislider
instanceRef={instance => {
if (instance && !ref) {
this.setState({ ref: instance });
}
}}
start={0}
range={{
min: 0,
max: 100
}}
/>
</div>
);
}
}
此代码不起作用给出错误 665行:“ ref”未定义为no-undef ................................................... ...................................................
答案 0 :(得分:0)
尝试这段代码,我只是重构您使用错误方式的ref。
您还可以使用输入并通过更新Slider上的启动道具来更改值。
class KeyboardSlider extends React.Component {
constructor(props){
super(props);
this.ref = React.CreateRef();
}
changeByRef = () => {
if (this.ref && this.ref.noUiSlider) {
this.ref.noUiSlider.set(20);
}
};
render() {
return (
<div>
<button onClick={this.changeByRef}>Change with ref</button>
<Nouislider
instanceRef={instance => {
if (instance && !ref) {
this.setState({ ref: instance });
}
}}
start={0}
range={{
min: 0,
max: 100
}}
/>
</div>
);
}