Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f7904d1

Browse files
committedJan 3, 2024
🍻 Implement day-11
1 parent d45668e commit f7904d1

13 files changed

+1967
-0
lines changed
 

‎day-11/reate-ts-pro/.eslintrc.cjs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
root: true,
3+
env: { browser: true, es2020: true },
4+
extends: [
5+
'eslint:recommended',
6+
'plugin:@typescript-eslint/recommended',
7+
'plugin:react-hooks/recommended',
8+
],
9+
ignorePatterns: ['dist', '.eslintrc.cjs'],
10+
parser: '@typescript-eslint/parser',
11+
plugins: ['react-refresh'],
12+
rules: {
13+
'react-refresh/only-export-components': [
14+
'warn',
15+
{ allowConstantExport: true },
16+
],
17+
},
18+
}

‎day-11/reate-ts-pro/.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

‎day-11/reate-ts-pro/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# React + TypeScript + Vite
2+
3+
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4+
5+
Currently, two official plugins are available:
6+
7+
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8+
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9+
10+
## Expanding the ESLint configuration
11+
12+
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
13+
14+
- Configure the top-level `parserOptions` property like this:
15+
16+
```js
17+
export default {
18+
// other rules...
19+
parserOptions: {
20+
ecmaVersion: 'latest',
21+
sourceType: 'module',
22+
project: ['./tsconfig.json', './tsconfig.node.json'],
23+
tsconfigRootDir: __dirname,
24+
},
25+
}
26+
```
27+
28+
- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
29+
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
30+
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list

‎day-11/reate-ts-pro/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React + TS</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>

‎day-11/reate-ts-pro/package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "reate-ts-pro",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "tsc && vite build",
9+
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
10+
"preview": "vite preview"
11+
},
12+
"dependencies": {
13+
"react": "^18.2.0",
14+
"react-dom": "^18.2.0"
15+
},
16+
"devDependencies": {
17+
"@types/react": "^18.2.43",
18+
"@types/react-dom": "^18.2.17",
19+
"@typescript-eslint/eslint-plugin": "^6.14.0",
20+
"@typescript-eslint/parser": "^6.14.0",
21+
"@vitejs/plugin-react": "^4.2.1",
22+
"eslint": "^8.55.0",
23+
"eslint-plugin-react-hooks": "^4.6.0",
24+
"eslint-plugin-react-refresh": "^0.4.5",
25+
"typescript": "^5.2.2",
26+
"vite": "^5.0.8"
27+
}
28+
}

‎day-11/reate-ts-pro/public/vite.svg

+1
Loading

‎day-11/reate-ts-pro/src/App.tsx

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import React, {useEffect, useRef, useState} from "react";
2+
3+
// 根据初始值自动推断
4+
// 场景:明确的初始值
5+
6+
type User = {
7+
name: string
8+
age: number
9+
}
10+
11+
// props 注解
12+
// type Props = {
13+
// className: string
14+
// }
15+
interface Props {
16+
className: string
17+
}
18+
19+
function Button(props: Props) {
20+
const {className} = props
21+
return <button className={className}>Click me</button>
22+
}
23+
24+
// props & typescript - 为 children 添加类型
25+
type Props2 = {
26+
className: string
27+
children: React.ReactNode
28+
}
29+
30+
function Button2(props: Props2) {
31+
const {className, children} = props
32+
return <button className={className}>{children}</button>
33+
}
34+
35+
// props & typescript - 为事件 prop 添加类型
36+
type Props3 = {
37+
onGetMsg?: (msg: string) => void
38+
}
39+
40+
function Son(props: Props3) {
41+
const {onGetMsg} = props
42+
const clickHandler = () => {
43+
onGetMsg?.('this is msg')
44+
}
45+
return <button onClick={clickHandler}>sendMsg</button>
46+
}
47+
48+
function App() {
49+
const [value, setValue] = useState(false)
50+
51+
const [list, setList] = useState([1, 2, 3])
52+
const changeValue = () => {
53+
setValue(true)
54+
}
55+
56+
const changeList = () => {
57+
setList([4, 5, 6])
58+
}
59+
60+
// 限制初始值的类型
61+
// const [user, setUser] = useState<User>({
62+
// name: 'jack',
63+
// age: 18
64+
// })
65+
66+
// const [user, setUser] = useState<User>(() => {
67+
// return {
68+
// name: 'jack',
69+
// age: 18
70+
// }
71+
// })
72+
73+
const [user, setUser] = useState<User>({
74+
name: 'jack',
75+
age: 18
76+
})
77+
78+
// const changeUser = () => {
79+
// setUser({
80+
// name: 'john',
81+
// age: 28
82+
// })
83+
// }
84+
const changeUser = () => {
85+
setUser(() => ({
86+
name: 'john',
87+
age: 28
88+
}))
89+
}
90+
91+
const [user2, setUser2] = useState<User | null>(null)
92+
93+
const changeUser2 = () => {
94+
setUser2(null)
95+
setUser2({
96+
name: 'jack',
97+
age: 18
98+
})
99+
}
100+
101+
// useRef & typescript
102+
const domRef = useRef<HTMLInputElement>(null);
103+
useEffect(() => {
104+
domRef.current?.focus()
105+
}, [])
106+
107+
108+
return (
109+
<>
110+
this is App{value}{list}
111+
<button onClick={changeValue}>1</button>
112+
<button onClick={changeList}>2</button>
113+
<br />
114+
115+
<button onClick={changeUser}>3</button>
116+
{user.name}
117+
<br />
118+
119+
{/* 为了类型安全, 可选链做类型守卫 */}
120+
{/* 只有 user 不为 null 时,才做点运算*/}
121+
{user2?.age}
122+
<button onClick={changeUser2}>4</button>
123+
<br />
124+
125+
<Button className="test"/>
126+
<br />
127+
128+
<Button2 className="test">children</Button2>
129+
<Button2 className="test">
130+
<span>this is span</span>
131+
</Button2>
132+
<br />
133+
134+
<Son onGetMsg={(msg) => console.log(msg)} />
135+
<br />
136+
137+
<input type="text" ref={domRef}/>
138+
<br />
139+
</>
140+
)
141+
}
142+
143+
export default App

‎day-11/reate-ts-pro/src/main.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import ReactDOM from 'react-dom/client'
2+
import App from './App.tsx'
3+
4+
ReactDOM.createRoot(document.getElementById('root')!).render(
5+
<App />
6+
)

‎day-11/reate-ts-pro/src/vite-env.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />

‎day-11/reate-ts-pro/tsconfig.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"useDefineForClassFields": true,
5+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
6+
"module": "ESNext",
7+
"skipLibCheck": true,
8+
9+
/* Bundler mode */
10+
"moduleResolution": "bundler",
11+
"allowImportingTsExtensions": true,
12+
"resolveJsonModule": true,
13+
"isolatedModules": true,
14+
"noEmit": true,
15+
"jsx": "react-jsx",
16+
17+
/* Linting */
18+
"strict": true,
19+
"noUnusedLocals": true,
20+
"noUnusedParameters": true,
21+
"noFallthroughCasesInSwitch": true
22+
},
23+
"include": ["src"],
24+
"references": [{ "path": "./tsconfig.node.json" }]
25+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"composite": true,
4+
"skipLibCheck": true,
5+
"module": "ESNext",
6+
"moduleResolution": "bundler",
7+
"allowSyntheticDefaultImports": true
8+
},
9+
"include": ["vite.config.ts"]
10+
}

‎day-11/reate-ts-pro/vite.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vite'
2+
import react from '@vitejs/plugin-react'
3+
4+
// https://vitejs.dev/config/
5+
export default defineConfig({
6+
plugins: [react()],
7+
})

‎day-11/reate-ts-pro/yarn.lock

+1,661
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.