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‘is not defined no-undef .
发布于 2019-12-13 08:08:29
试试这段代码,我只是重构了一下,你使用ref的方式是错误的。
也可以使用输入并通过更新滑块上的开始道具来更改该值。
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>
);
}
https://stackoverflow.com/questions/56601231
复制相似问题