How to start search only when user stops typing?

后端 未结 10 1776

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 18:59

    you can just use the debounce from lodash or simulate using setTimeout.

    import React, {Component, PropTypes} from 'react';
    
    export default class SearchBox extends Component {
        constructor(props){
           super(props);
           this.state={ name:" "}
           this.timeout =  null;
    
        }
    
        changeName = (event) => {
            clearTimeout(timeout);
             if(timeout){
               setTimeout((event)=> this.setState({name: event.target.value}), 200)
             }
        }
    
        sendToParent = () => {
            this.props.searching(this.state.name);
        }
    
        render() {
            return (
                
    ); } }

提交回复
热议问题