How to start search only when user stops typing?

后端 未结 10 1787

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:09

    You can use setTimeout with respect to your code as follows,

    state = {
        name: '',
        typing: false,
        typingTimeout: 0
    }
    changeName = (event) => {
        const self = this;
    
        if (self.state.typingTimeout) {
           clearTimeout(self.state.typingTimeout);
        }
    
        self.setState({
           name: event.target.value,
           typing: false,
           typingTimeout: setTimeout(function () {
               self.sendToParent(self.state.name);
             }, 5000)
        });
    }
    

    Also, you need to bind changeName handler function in constructor.

    constructor(props) {
       super(props);
       this.changeName = this.changeName.bind(this);
    }
    

提交回复
热议问题