// src/components/StatefulHello.tsximport * as React from "react";export interface Props { name: string; enthusiasmLevel?: number;}interface State { currentEnthusiasm: number;}class Hello extends React.Component { constructor(props: Props) { super(props); this.state = { currentEnthusiasm: props.enthusiasmLevel || 1 }; } onIncrement = () => this.updateEnthusiasm(this.state.currentEnthusiasm + 1); onDecrement = () => this.updateEnthusiasm(this.state.currentEnthusiasm - 1); render() { const { name } = this.props; if (this.state.currentEnthusiasm <= 0) { throw new Error('You could be a little more enthusiastic. :D'); } return (
Hello {name + getExclamationMarks(this.state.currentEnthusiasm)}
); } updateEnthusiasm(currentEnthusiasm: number) { this.setState({ currentEnthusiasm }); }}export default Hello;function getExclamationMarks(numChars: number) { return Array(numChars + 1).join('!');}