只要用户在打字,我就不想发出请求.我的代码应该限制请求,这样当用户快速键入时,它将使用最新的输入值触发一个请求,而不是多个请求.
目前,当我输入"测试"时,它会触发4个不同的请求:
- "t"
- "te"
- "tes"
- "测试"
So I found lodash _.debounce and _.throttle ( [https://lodash.com/docs/4.17.4#debounce] ) but don't really understand how I can inplement it to my code. Can anyone help me?
我的代码:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import './style.css';
import { search } from '../../actions/';
class SearchBar extends Component {
constructor(props) {
super(props);
this.state = { searchTerm: '' };
}
startSearch(query) {
const storedTerms = this.props.searchedTerm;
let foundDuplicate = false;
if (storedTerms.length === 0 && query) {
return this.props.search(query);
}
if (storedTerms.length !== 0 && query) {
const testDuplicate = storedTerms.map(term => term === query);
foundDuplicate = testDuplicate.some(element => element);
}
if (storedTerms.length !== 0 && !query) {
return false;
}
if (foundDuplicate) {
return false;
}
return this.props.search(query);
}
handleInputChange(term) {
this.setState({ searchTerm: term });
this.startSearch(term);
}
render() {
return (
<div className="Search-bar">
<input
value={this.state.searchTerm}
onChange={event => this.handleInputChange(event.target.value)}
/>
</div>
);
}
function mapStateToProps(state) {
return {
searchedTerm: state.searchedTerm,
savedData: state.savedData,
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ search }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(SearchBar);
编辑:
Thx给Sagiv b.g,我要补充一些解释:
好的,所以用户应该输入两个以上的字母&&;此外,我的应用程序在启动ajax请求之前应该至少等待2秒钟
EDIT2: Thx to Sagiv b.g, for great solution!
我对代码做了如下更改:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import _ from 'lodash';
import './style.css';
import { search } from '../../actions/';
class SearchBar extends Component {
constructor(props) {
super(props);
this.state = { inputValue: '' };
this.startSearch = _.debounce(this.startSearch, 2000);
}
startSearch(query) {
const storedTerms = this.props.searchedTerm;
let foundDuplicate = false;
if (storedTerms.length === 0 && query) {
return this.props.search(query);
}
if (storedTerms.length !== 0 && query) {
const testDuplicate = storedTerms.map(term => term === query);
foundDuplicate = testDuplicate.some(element => element);
}
if (storedTerms.length !== 0 && !query) {
return false;
}
if (foundDuplicate) {
return false;
}
return this.props.search(query);
}
onChange = ({ target: { value } }) => {
this.setState({ inputValue: value });
if (value.length > 2) {
this.startSearch(value);
}
};
render() {
return (
<div className="Search-bar">
<input
placeholder="Type something to search GitHub"
value={this.state.inputValue}
onChange={this.onChange}
/>
</div>
);
}
}
function mapStateToProps(state) {
return {
searchedTerm: state.searchedTerm,
savedData: state.savedData,
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ search }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(SearchBar);
Last Bug to deal with
但它还有最后一个错误,我不知道如何摆脱.当用户想要更改搜索查询并使用backspace删除搜索字段时,我的应用程序总是意外地触发另一个API请求.
Any ideas how I can get rid of that behavior?