Start with React

claireliao
3 min readOct 4, 2021

--

Install by npx tool

//Create React.js project 
npx create-react-app [APP_NAME]
//Create a new React project with TypeScript
npx create-react-app [APP_NAME] --template typescript
//Create React.js project with Redux
npx create-react-app [APP_NAME] --template redux
//Create a new React project with Redux & TypeScript
npx create-react-app [APP_NAME] --template redux-typescript

Initial File Structure

Create by:

npx create-react-app [APP_NAME] --template redux-typescript

File Structure:

├── README.md 
├── node_modules
├── package.json
├── .gitignore
├── tsconfig.json
├── public
│ ├── favicon.ico
│ ├── index.html
│ ├── logo192.png
│ ├── logo512.png
│ ├── robots.txt
│ └── manifest.json
└── src
├── app
│ ├── hooks.ts
│ └── store.ts
├── features/counter
│ ├── Counter.module.css
│ ├── Counter.tsx
│ ├── counterAPI.ts
│ ├── counterSlice.spec.ts
│ └── counterSlice.ts
├── App.css
├── App.tsx
├── App.test.tsx
├── index.css
├── index.tsx
├── logo.svg
├── react-app-env.d.ts
├── setupTests.ts
└── serviceWorker.ts

Entrance of React

Before we start to write React, we must know how it works. So, let’s look into “public/index.html” & “src/index.tsx” these two files.

public/index.html

<!DOCTYPE html> 
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<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" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!-- manifest.json provides metadata used when your web app is installed on a user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ --> <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!-- Notice the use of %PUBLIC_URL% in the tags above. It will be replaced with the URL of the `public` folder during the build. Only files inside the `public` folder can be referenced from the HTML. Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will work correctly both with client-side routing and a non-root public URL. Learn how to configure a non-root public URL by running `npm run build`. -->
<title>React Redux App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app. </noscript>
<div id="root"></div>
<!-- This HTML file is a template. If you open it directly in the browser, you will see an empty page. You can add webfonts, meta tags, or analytics to this file. The build step will place the bundled scripts into the <body> tag. To begin the development, run `npm start` or `yarn start`. To create a production bundle, use `npm run build` or `yarn build`. -->
</body>
</html>

In “public/index.html”, defines the basic template of React website. If you would like to change the logo or title of the website, you need to change it here.

Change title

<title>React Redux App</title>

Change logo

src/index.tsx

import React from 'react'; 
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { store } from './app/store';
import { Provider } from 'react-redux';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById('root')
);
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

Did anyone discover how index.tsx insert React project into index.html? If not, let’s take a look right now.

public/index.html src/index.tsx

document.getElementById('root')

All the components we write in React, will be placed into div with id=”root”.

This is the end of this post! Hope you can not only just know how to CRA but also learn something.

Originally published at http://github.com.

hackmd version

--

--