Javascript 在更新输入时防止不必要的请求
如何在更新输入时防止不必要的请求 我尝试了下面的解决方案。但是在Javascript 在更新输入时防止不必要的请求,javascript,reactjs,axios,Javascript,Reactjs,Axios,如何在更新输入时防止不必要的请求 我尝试了下面的解决方案。但是在应用程序文件中,声明了搜索,但从未使用过。我试过类似的方法。 文件utils.js中的变量let-token是什么。我是否应该将let token=localStorage.getItem('token')分配给此变量 应用程序 import search from /.utils class App extends Component { constructor (props) { super(props);
应用程序
文件中,声明了搜索
,但从未使用过。我试过类似的方法。
文件utils.js
中的变量let-token
是什么。我是否应该将let token=localStorage.getItem('token')分配给此变量代码>
应用程序
import search from /.utils
class App extends Component {
constructor (props) {
super(props);
this.state = {
todos: [],
}
}
search = (query) => {
axios({
url: `/api/v1/todos/{query}`,
method: "GET"
})
.then(res => {
this.setState({
todos: res.data
});
})
.catch(error => {
console.log(error);
})
render () {
return (
<input onChange={this.search} />
)
}
}
您可以使用延迟onChange执行的函数来实现这一点
// _.debounce(yourSearch function, delay time);
search(e){
let str = e.target.value;
_.debounce(() => yourFunction, 500);
}
我建议使用去Bouncing,因此不要在每次输入更改时都使用API。具体地说,您可以延迟onChange函数的调用,直到指定的超时过期为止。这可能会对你有所帮助。你的两个代码片段有何关联?@GabrielePetrioli in utils.js exportsearch
,我在应用程序中使用assearch=(query)=>{
…@Umbro不,你不用它。你只需创建一个同名类的方法。我有一个问题:我做了:从'lodash'导入{debounce};This.search1(查询){uu.debounce(()=>this.search(query),500);}
但它不起作用。我为您制作了一个要点文件。看一下。如何导入它?
// _.debounce(yourSearch function, delay time);
search(e){
let str = e.target.value;
_.debounce(() => yourFunction, 500);
}