Created
December 31, 2016 02:38
-
-
Save adrianmcli/fc4cdc437f38f4c87d18db569f396db0 to your computer and use it in GitHub Desktop.
A simple React component to show how to add a time-delayed typing indicator for a textarea.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
class IsTyping extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
typing: false, | |
}; | |
this.timeout = null; | |
this.onChange = this.onChange.bind(this); | |
} | |
onChange() { | |
this.setState({ typing: true }); | |
// cancel previous timeout if it exists | |
if (this.timeout) { | |
clearTimeout(this.timeout); | |
} | |
// set typing to false after one second | |
this.timeout = setTimeout(() => { | |
this.setState({ typing: false }); | |
}, 1000); | |
} | |
render() { | |
return ( | |
<div> | |
<textarea name="" id="" cols="30" rows="10" onChange={this.onChange}> | |
</textarea> | |
{this.state.typing ? <div>Typing...</div> : null} | |
</div> | |
); | |
} | |
} | |
export default IsTyping; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment