How to start search only when user stops typing?

后端 未结 10 1788

I need to perform a Search when user stops typing.I know I am supposed to use setTimeout() . But with Reactjs I cant find how it works. Can

10条回答
  •  不思量自难忘°
    2020-12-07 19:15

    Another way that worked with me:

    class Search extends Component {
      constructor(props){
        super(props);
        this.timeout =  0;
      }
    
      doSearch(evt){
        var searchText = evt.target.value; // this is the search text
        if(this.timeout) clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          //search function
        }, 300);
      }
    
       render() {
        return (
          
    this.doSearch(evt)} />
    ); } }

提交回复
热议问题