import React, {Component} from 'react'; 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 = () => { let newState = { color: "red" }; this.setState(newState); }; onBlueClick = () => { let newState = { color: "blue" }; this.setState(newState); }; onGreenClick = () => { let newState = { color: "green" }; this.setState(newState); }; render() { return (

); } } export default App;