import React, {Component} from 'react'; import ColorTitle from "./ColorTitle"; interface AppState { color: string // The current color that's selected by the user. } class App extends Component<{}, AppState> { canvasRef: any // Could write React.RefObject if we wanted. constructor(props: {}) { super(props); this.canvasRef = React.createRef(); this.state = { color: "blue" }; } componentDidUpdate() { this.updateCanvasImage(); } componentDidMount() { this.updateCanvasImage(); } updateCanvasImage() { let canvas = this.canvasRef.current; let context = canvas.getContext("2d"); context.fillStyle = this.state.color; context.fillRect(50, 50, 150, 100); } onRedClick = () => { this.setState({ color: "red" }); }; onBlueClick = () => { this.setState({ color: "blue" }); }; onGreenClick = () => { this.setState({ color: "green" }); }; render() { return (

); } } export default App;