简体   繁体   English

在React js中搜索lodash.debounce

[英]lodash.debounce search in react js

I'm trying to debounce a search in react using lodash, debounce. 我正在尝试使用lodash和debounce消除反应中的搜索。

But when ever its run I'm receiving a type error 但是,无论何时运行,我都会收到类型错误

TypeError: Cannot read property 'debounce' of undefined

I have tried moving it around in the code but can't understand why it isn't working 我试图在代码中移动它,但不明白为什么它不起作用

I started off by importing it 我从导入开始

import { _, debounce } from 'lodash'

I have an input as following 我有如下输入

<input
    type="text"
    value={this.state.query}
    onChange={(e) => {this.updateQuery(e.target.value)}}
    placeholder="Search by title or author"
/>

Connected to this function 连接到此功能

updateQuery = (query) => {
    _.debounce(() => query(this.setState({ query }), 500))
    this.onBookSearch(query)
}

Does anyone understand why this is? 有人知道这是为什么吗?

I think your import is the problem. 我认为您的导入是问题所在。 You probably want to import the _ as default: 您可能想将_导入为默认值:

import _, {debounce} from 'lodash';

Also you're not using the extracted function: 另外,您没有使用提取的功能:

updateQuery = (query) => {
  debounce(() => query(this.setState({ query }), 500))
  this.onBookSearch(query)
}

Since you're extracting {debounce} in the import you can use it directly. 由于您要在导入中提取{debounce} ,因此可以直接使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM