React with TypeScript

I made a game of Tic Tac Toe on React with TypeScript instead of JavaScript. You can follow this official tutorial of React to build it with JavaScript. Then you can convert it to TypeScript with this handbook.
Tic Tac Toe is a paper-and-pencil game for two players who take turns marking the spaces in a three-by-three grid with X or O. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner.
React is a front-end JavaScript library for building user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. React can be used as a base in the development of single-page or mobile applications. It is a popular front-end framework. Many companies require their front-end developers to master it.
TypeScript is a programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. TypeScript is designed for the development of large applications and transcompiles to JavaScript. As TypeScript is a superset of JavaScript, existing JavaScript programs are also valid TypeScript programs. The TypeScript compiler is itself written in TypeScript and compiled to JavaScript. Anders Hejlsberg, the lead architect of C# and creator of Delphi and Turbo Pascal, has worked on the development of TypeScript.
I think TypeScript is better than JavaScript. It is also a good combination with React as a popular front-end framework. Now, let's code it.
Using TypeScript with Create React App
Let's get started by creating a React app with TypeScript.
npx create-react-app my-app --template typescript
Install Yarn CLI by using the command npm install --global yarn. Then, we can start development by using the command yarn start. It will recompile our React app every time it has some changes.
CSS
We focus on two files; index.tsx and App.tsx. Let's create CSS files for them; index.css and App.css.
1 2 3 4 5 6 7 8 |
|
The index.css is to reset styles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
The App.css is to style the main app.
TSX
The .ts is the file extension for TypeScript files. The .tsx is used when we want to embed JSX elements inside the files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
We leave it default for index.tsx. Just make sure it renders the app.
App
We code the main application on the App.tsx.
Interfaces
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
We created 3 object types; SquareProps, BoardProps, and GameState.
Calculating Winner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Here is the function to calculate the winner.
Square
1 2 3 4 5 6 7 8 9 |
|
We just rendered a button as a cell of the board.
Board
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
The board has 9 cells or squares.
Game
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
We render the board or game, set history, handle a click, then calculate the winner on every move or every time the square got clicked.
Closing
Mastering React and TypeScript is the best choice to be a Front-End expert. React is so popular even some companies put it on the interview. TypeScript is the future and the best choice to handle large projects or work with many developers because it is the only strongly typed programming language I have known for the front-end.
I suggest you master TypeScript first before going further with its development. At least you had a TypeScript crash course. Then you will be able to master its frameworks such as React and Angular.
Maybe I will make a crash course for TypeScript in the future. You can find the source code of this tutorial at https://github.com/aristorinjuang/typescript-react.
References
- https://en.wikipedia.org/wiki/Tic-tac-toe
- https://en.wikipedia.org/wiki/React_(JavaScript_library)
- https://en.wikipedia.org/wiki/React_Native
- https://en.wikipedia.org/wiki/TypeScript
Related Articles
- React Context with TypeScript
- Convert Any Images to JPEG and WebP, Compress, and Resize on the Client Side
- Angular vs React vs Vue.js
- Create a Private NodeJS Module with TypeScript