Accessing event.target inside callback in react

前端 未结 4 1542
借酒劲吻你
借酒劲吻你 2020-11-28 14:00

I have the following class snippet:

constructor(props) {
    super(props);

    this.timeout = null;
}

search = (e) => {
    clearTimeout(this.timeout);
         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 14:49

    Are you going to make the timeout as state?

    My suggestion would be like this

    constructor(props) {
        super(props);
        this.state = {
           timeout: null
        }
    }
    
    search = (e) => {
        clearTimeout(this.state.timeout);
        const timeout = setTimeout(
            function(){ console.log(e.target); },
            500
        );
        this.setState({timeout: timeout})
    
    }
    

提交回复
热议问题