visit
Let’s start with the project creation. I’ll use create-react-app
with typescript
template
npx create-react-app my-app --template typescript
. This command will create an initial app with all basic dependencies. After successful installation we will see the next structure:
I’ll clean and remove non-used files a little bit later. Not let’s add prettier
to the project. This will give us the ability to format our code.
npm install --save husky lint-staged prettier
This command will install prettier, husky and lint- staged, which will help us with formatting the code on during pre-commit or pre-push phase.
I always use this basic configuration of .prettierrc
file:
{
"printWidth": 100,
"tabWidth": 4,
"singleQuote": true,
"trailingComma": "es5",
"arrowParens": "always"
}
Then let’s add some new lines to package.json
file:
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"src/**/*.{ts,tsx,css}": [
"prettier --write"
]
}
I also want to have a separate command to run linter. Eslint is already included from create-react-app
. So I just add additional command to scripts section in package.json
file:
"lint": "eslint \"./src/**/*.{ts,tsx}\""
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"pre-push": "npm run lint"
}
}
The next thing is to update some files and remove parts that we don’t need.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<title>Checkers</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
body {
margin: 0;
padding: 0;
}
import React from 'react';
import './App.css';
function App() {
return <div className="App">Checkers Game</div>;
}
export default App;
Now let’s check that everything works and execute npm run start
We’ll see our empty page with the text “Checkers Game“