Lodash debounce not working in React

前端 未结 3 1143
别那么骄傲
别那么骄傲 2021-02-05 04:09

it would be best to first look at my code:

import React, { Component } from \'react\';
import _ from \'lodash\';
import Services from \'Services\'; // Webser         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-05 04:23

    The problem occurs because you aren't calling the debounce function, you could do in the following manner

    export default class componentName extends Component {
      constructor(props) {
        super(props);
        this.state = {
          value: this.props.value || null
        }
        this.servicesValue = _.debounce(this.servicesValue, 1000);
      }
    
      onChange(value) {
        this.setState({ value });
        this.servicesValue(value);
      }
      servicesValue = (value) => {
          Services.setValue(value)
      }
      render() {
        return (
          
    this.onChange(value)} value={this.state.value} />
    ) } }

提交回复
热议问题