【问题标题】:Nouislider reactjs set valueNouislider reactjs 设置值
【发布时间】:2019-10-29 06:31:26
【问题描述】:

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 ..................................................... .....................................................

【问题讨论】:

  • 我知道这是旧的,但你设法让它工作了吗?

标签: reactjs nouislider


【解决方案1】:

试试这段代码,我只是重构你以错误的方式使用 ref。

您还可以使用输入并通过更新 Slider 上的 start 属性来更改值。

 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>
    );
  }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
相关资源
最近更新 更多