Skip to content

Instantly share code, notes, and snippets.

@nknighta
Created July 10, 2023 03:18
Show Gist options
  • Save nknighta/3e6179aadb203d695a7dd02e8f338537 to your computer and use it in GitHub Desktop.
Save nknighta/3e6179aadb203d695a7dd02e8f338537 to your computer and use it in GitHub Desktop.
import React from 'react';
interface TextBoxState {
text: string;
}
class TextBox extends React.Component<{}, TextBoxState> {
constructor(props: {}) {
super(props);
this.state = {
text: '',
};
}
componentDidMount() {
const savedText = localStorage.getItem('savedText');
if (savedText) {
this.setState({ text: savedText });
}
}
componentDidUpdate(_: {}, prevState: TextBoxState) {
if (prevState.text !== this.state.text) {
localStorage.setItem('savedText', this.state.text);
}
}
handleChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
this.setState({ text: event.target.value });
};
render() {
return (
<textarea value={this.state.text} onChange={this.handleChange} />
);
}
}
export default TextBox;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment