import React, { Component } from 'react'; class App extends Component<{}, {}> { canvasRef: React.RefObject; constructor(props: {}) { super(props); this.canvasRef = React.createRef(); } componentDidMount() { this.updateCanvasImage(); } updateCanvasImage() { let canvas = this.canvasRef.current; if (canvas === null) return; let context = canvas.getContext("2d"); if (context === null) return; context.fillStyle = "blue"; context.fillRect(50, 50, 150, 100); } // For things like button handlers, we need to use arrow functions instead of regular functions. // The rule: Any time you pass a function as a value to something else, that function needs to // be an arrow function. handleButtonClick = () => { alert("Ouch!"); // alert() is built into browsers - displays a pop-up message }; render() { // We can only return one thing from render. So if we want to return more than one thing, we // wrap them together inside a
, and return the div. return (
); } } export default App;