Skip to content

Instantly share code, notes, and snippets.

@adrianmcli
Created December 31, 2016 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianmcli/fc4cdc437f38f4c87d18db569f396db0 to your computer and use it in GitHub Desktop.
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.
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