为了账号安全,请及时绑定邮箱和手机立即绑定

我的 lodash 反跳功能不适用于输入和 Reacttsx

我的 lodash 反跳功能不适用于输入和 Reacttsx

繁星点点滴滴 2023-09-04 16:03:23
我将 lodash debounce 添加到我的输入中,我想使用它。我用 console.logs 检查了方法,但它从未进入去抖部分。const debounce = require('lodash/debounce');filterFeed(event: any){    // event.preventDefault()    event.persist();    debounce(() => {        let searchString = event.target.value;        let unfilteredFeed: any = this.unfilteredFeed;        let filteredFeed: any = unfilteredFeed.filter(el => el.title.toLowerCase().includes(event.target.value.toLowerCase()));        this.setState({ input: searchString, feed: filteredFeed, filter: true })     }, 300);} <input className="basic-slide" id="name" type="text" value={this.state.input} placeholder="Event..." onChange={ this.filterFeed.bind(this)} /><label >Suche</label>
查看完整描述

3 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

您可以使用解决方法:


filterFeed = (value) => {//Pass the value, not the event   

    let searchString = value

    let unfilteredFeed = this.unfilteredFeed;

    let filteredFeed = unfilteredFeed.filter(el => el.title.toLowerCase().includes(value.toLowerCase()));

    this.setState({

      input: searchString,

      feed: filteredFeed,

      filter: true

    })

  }

还有你的 JSX:


<input className="basic-slide" id="name" type="text" value={this.state.input} placeholder="Event..." onChange={(e)=>{

          const value = e.target.value;

          this.debouncedFilterFeed(value)

          }} /><label >Suche</label>


查看完整回答
反对 回复 2023-09-04
?
守着星空守着你

TA贡献1799条经验 获得超8个赞

在尝试了不同的事情之后,我找到了我的解决方案:


filterFeed= debounce((text) =>{

    let searchString = text;

    let unfilteredFeed: any = this.unfilteredFeed;

    let filteredFeed: any = unfilteredFeed.filter(el => el.title.toLowerCase().includes(searchString.toLowerCase()));

    this.setState({ input: searchString, feed: filteredFeed, filter: true })

}, 20)


<input className="basic-slide" id="name" type="text" value={this.state.input} placeholder="Event..." onChange={e  => this.filterFeed(e.target.value)} /><label >Suche</label>



查看完整回答
反对 回复 2023-09-04
?
慕哥6287543

TA贡献1831条经验 获得超10个赞

您需要将传递给 debounce 的整个函数包装起来onChange并保存其引用:


filterFeed = (event: any) => {

  // event.preventDefault()

  event.persist();

  let searchString = event.target.value;

  let unfilteredFeed: any = this.unfilteredFeed;

  let filteredFeed: any = unfilteredFeed.filter(el => el.title.toLowerCase().includes(event.target.value.toLowerCase()));

  this.setState({

    input: searchString,

    feed: filteredFeed,

    filter: true

  })

}


debouncedFilterFeed = debounce(this.filterFeed, 300);



<input className="basic-slide" id="name" type="text" value={this.state.input} placeholder="Event..." onChange={this.debouncedFilterFeed} /><label >Suche</label>


查看完整回答
反对 回复 2023-09-04
  • 3 回答
  • 0 关注
  • 102 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信