Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在更新输入时防止不必要的请求_Javascript_Reactjs_Axios - Fatal编程技术网

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 export
search
,我在应用程序中使用as
search=(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);
    }