3 回答
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>
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>
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>
- 3 回答
- 0 关注
- 102 浏览
添加回答
举报